~ubuntu-branches/ubuntu/natty/bluefish/natty-proposed

« back to all changes in this revision

Viewing changes to src/authen.c

  • Committer: Bazaar Package Importer
  • Author(s): Davide Puricelli (evo)
  • Date: 2005-04-23 17:05:18 UTC
  • mto: (1.1.5 upstream) (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050423170518-izh2k25xve7ui1jx
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Bluefish HTML Editor
 
2
 * authen.c - handle authentication requests for gnome-vfs
 
3
 *
 
4
 * Copyright (C) 2004 Salvador Fandino
 
5
 * changes/bugfixes (C) 2004 Olivier Sessink
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 */
 
21
#define DEBUG
 
22
 
 
23
#include <gtk/gtk.h>
 
24
#include <string.h>
 
25
#include "bluefish.h"
 
26
 
 
27
#include "authen.h"
 
28
#include "gtk_easy.h"
 
29
 
 
30
#ifdef HAVE_ATLEAST_GNOMEVFS_2_6
 
31
#ifndef HAVE_ATLEAST_GNOMEUI_2_6
 
32
 
 
33
#include <libgnomevfs/gnome-vfs-standard-callbacks.h>
 
34
 
 
35
typedef struct {
 
36
        GHashTable *hash;
 
37
} AuthenData;
 
38
 
 
39
typedef struct {
 
40
        gchar *user;
 
41
        gchar *passwd;
 
42
} AuthenValue;
 
43
 
 
44
static void authen_value_free(AuthenValue * val)
 
45
{
 
46
        g_free(val->user);
 
47
        g_free(val->passwd);
 
48
        g_free(val);
 
49
}
 
50
 
 
51
static AuthenValue *authen_value_new(gchar * user, gchar * passwd)
 
52
{
 
53
        AuthenValue *val = g_new0(AuthenValue, 1);
 
54
        val->user = g_strdup(user);
 
55
        val->passwd = g_strdup(passwd);
 
56
        return val;
 
57
}
 
58
 
 
59
/**
 
60
 * authen_ask_user
 
61
 *
 
62
 * display authentication request
 
63
 *
 
64
 * returns true if the user accepted to authenticate
 
65
 */
 
66
 
 
67
static gint authen_ask_user(gchar const *uri, gchar const *object, gchar const *user,
 
68
                                                        gchar ** newuser, gchar ** newpasswd)
 
69
{
 
70
        GtkWidget *dialog, *box, *label;
 
71
        GtkWidget *textuser, *textpasswd;
 
72
        gint ret;
 
73
        gchar *labelstr;
 
74
        dialog =
 
75
                gtk_dialog_new_with_buttons(_("Authentication required"), NULL,
 
76
                                                                        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
 
77
                                                                        GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
 
78
                                                                        GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
 
79
        gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
 
80
        gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
 
81
        box = gtk_vbox_new(FALSE, 5);
 
82
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), box);
 
83
        gtk_container_set_border_width(GTK_CONTAINER(box), 12);
 
84
 
 
85
        labelstr = g_strdup_printf(_("Authentication is required for %s."), uri);
 
86
        label = gtk_label_new(labelstr);
 
87
        g_free(labelstr);
 
88
        gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
 
89
        gtk_container_add(GTK_CONTAINER(box), label);
 
90
 
 
91
        label = gtk_label_new(_("Username:"));
 
92
        gtk_container_add(GTK_CONTAINER(box), label);
 
93
 
 
94
        textuser = boxed_entry_with_text(user, 80, box);
 
95
 
 
96
        label = gtk_label_new(_("Password:"));
 
97
        gtk_container_add(GTK_CONTAINER(box), label);
 
98
 
 
99
        textpasswd = boxed_entry_with_text("", 80, box);
 
100
        gtk_entry_set_visibility(GTK_ENTRY(textpasswd), FALSE);
 
101
 
 
102
        gtk_widget_show_all(dialog);
 
103
        ret = gtk_dialog_run(GTK_DIALOG(dialog));
 
104
        
 
105
        DEBUG_MSG("authen_ask_user: dialog return ret = %d ok = %d\n", ret, (ret == GTK_RESPONSE_ACCEPT));
 
106
        if (ret == GTK_RESPONSE_ACCEPT) {
 
107
                *newuser = gtk_editable_get_chars(GTK_EDITABLE(textuser), 0, -1);
 
108
                *newpasswd = gtk_editable_get_chars(GTK_EDITABLE(textpasswd), 0, -1);
 
109
                DEBUG_MSG("authen_ask_user: newuser=%s\n",*newuser);
 
110
                gtk_widget_destroy(dialog);
 
111
                return TRUE;
 
112
        }
 
113
        gtk_widget_destroy(dialog);
 
114
        return FALSE;
 
115
}
 
116
 
 
117
static gchar *make_key(gchar * proto, gchar * server, int port, gchar * object)
 
118
{
 
119
        gchar *str = g_strdup_printf("proto=%s server=%s port=%d object=%s", (proto ? proto : ""),
 
120
                                        (server ? server : ""), port, (object ? object : ""));
 
121
        DEBUG_MSG("authen_callback: key = %s\n", str);
 
122
        return str;
 
123
}
 
124
 
 
125
static void fill_authen_callback(GnomeVFSModuleCallbackFillAuthenticationIn * in, gsize in_size,
 
126
                                                                 GnomeVFSModuleCallbackFillAuthenticationOut * out, gsize out_size,
 
127
                                                                 AuthenData * data)
 
128
{
 
129
        gchar *key;
 
130
        AuthenValue *val;
 
131
 
 
132
        DEBUG_MSG("fill_authen_callback: uri = %s\n", in->uri);
 
133
 
 
134
        key = make_key(in->protocol, in->server, in->port, in->object);
 
135
        if ((val = (AuthenValue *) (g_hash_table_lookup(data->hash, key))) != NULL) {
 
136
                DEBUG_MSG("fill_authen_callback: key found on authen cache\n");
 
137
                out->valid = TRUE;
 
138
                out->username = g_strdup(val->user);
 
139
                out->password = g_strdup(val->passwd);
 
140
                DEBUG_MSG("fill_authen_callback: user=%s len(passwd)=%d\n", out->username, strlen(out->password));
 
141
        }
 
142
        g_free(key);
 
143
}
 
144
 
 
145
static void full_authen_callback(GnomeVFSModuleCallbackFullAuthenticationIn * in, gsize in_size,
 
146
                                                                 GnomeVFSModuleCallbackFullAuthenticationOut * out, gsize out_size,
 
147
                                                                 AuthenData * data)
 
148
{
 
149
 
 
150
/*      gchar *key; */
 
151
 
 
152
        DEBUG_MSG("full_authen_callback: uri = %s flags = 0x%x\n", in->uri, in->flags);
 
153
 
 
154
/*      key = make_key(in->protocol, in->server, in->port, in->object);
 
155
        g_hash_table_remove(data->hash, key);
 
156
        g_free(key);*/
 
157
 
 
158
        if (authen_ask_user(in->uri, in->object, in->default_user, &(out->username), &(out->password))) {
 
159
/*              g_hash_table_replace(data->hash, key, authen_value_new(out->username, out->password));*/
 
160
                out->domain = in->default_domain;
 
161
                out->save_password = FALSE;
 
162
                out->abort_auth = FALSE;
 
163
                out->keyring = NULL;
 
164
        } else
 
165
                out->abort_auth = TRUE;
 
166
        DEBUG_MSG("full_authen_callback: user = %s len(passwd)=%d\n", out->username, strlen(out->password));
 
167
}
 
168
 
 
169
static void save_authen_callback(GnomeVFSModuleCallbackSaveAuthenticationIn * in, gsize in_size,
 
170
                                                                 GnomeVFSModuleCallbackSaveAuthenticationOut * out, gsize out_size,
 
171
                                                                 AuthenData * data)
 
172
{
 
173
        gchar *key;
 
174
        AuthenValue *val;
 
175
 
 
176
        DEBUG_MSG("save_authen_callback: uri = %s keyring = %s\n", in->uri, in->keyring);
 
177
 
 
178
        key = make_key(in->protocol, in->server, in->port, in->object);
 
179
        val = authen_value_new(in->username, in->password);
 
180
        g_hash_table_replace(data->hash, key, val);
 
181
}
 
182
 
 
183
/*
 
184
 * set_authen_callbacks:
 
185
 * 
 
186
 * register gnome-vfs callbacks for user authentication handling
 
187
 */
 
188
 
 
189
void set_authen_callbacks(void)
 
190
{
 
191
 
 
192
        AuthenData *data = g_new0(AuthenData,1);
 
193
        data->hash =
 
194
                g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify) authen_value_free);
 
195
/*      gnome_vfs_module_callback_set_default(GNOME_VFS_MODULE_CALLBACK_FILL_AUTHENTICATION,
 
196
                                                                                  (GnomeVFSModuleCallback) fill_authen_callback, data,
 
197
                                                                                  NULL);
 
198
        gnome_vfs_module_callback_set_default(GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION,
 
199
                                                                                  (GnomeVFSModuleCallback) full_authen_callback, data,
 
200
                                                                                  NULL);
 
201
        gnome_vfs_module_callback_set_default(GNOME_VFS_MODULE_CALLBACK_SAVE_AUTHENTICATION,
 
202
                                                                                  (GnomeVFSModuleCallback) save_authen_callback, data,
 
203
                                                                                  NULL);*/
 
204
        gnome_vfs_module_callback_push (GNOME_VFS_MODULE_CALLBACK_FILL_AUTHENTICATION,
 
205
                                        (GnomeVFSModuleCallback) fill_authen_callback, 
 
206
                                        data,
 
207
                                        NULL);
 
208
        gnome_vfs_module_callback_push (GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION,
 
209
                                        (GnomeVFSModuleCallback) full_authen_callback, 
 
210
                                        data,
 
211
                                        NULL);
 
212
        gnome_vfs_module_callback_push (GNOME_VFS_MODULE_CALLBACK_SAVE_AUTHENTICATION,
 
213
                                        (GnomeVFSModuleCallback) save_authen_callback, 
 
214
                                        data,
 
215
                                        NULL);
 
216
        DEBUG_MSG("set_authen_callbacks: callbacks registered\n");
 
217
 
 
218
}
 
219
 
 
220
#endif /*  */
 
221
#endif                                                  /* HAVE_ATLEAST_GNOMEVFS_2_6 */