1
/* Bluefish HTML Editor
2
* authen.c - handle authentication requests for gnome-vfs
4
* Copyright (C) 2004 Salvador Fandino
5
* changes/bugfixes (C) 2004 Olivier Sessink
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.
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.
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
30
#ifdef HAVE_ATLEAST_GNOMEVFS_2_6
31
#ifndef HAVE_ATLEAST_GNOMEUI_2_6
33
#include <libgnomevfs/gnome-vfs-standard-callbacks.h>
44
static void authen_value_free(AuthenValue * val)
51
static AuthenValue *authen_value_new(gchar * user, gchar * passwd)
53
AuthenValue *val = g_new0(AuthenValue, 1);
54
val->user = g_strdup(user);
55
val->passwd = g_strdup(passwd);
62
* display authentication request
64
* returns true if the user accepted to authenticate
67
static gint authen_ask_user(gchar const *uri, gchar const *object, gchar const *user,
68
gchar ** newuser, gchar ** newpasswd)
70
GtkWidget *dialog, *box, *label;
71
GtkWidget *textuser, *textpasswd;
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);
85
labelstr = g_strdup_printf(_("Authentication is required for %s."), uri);
86
label = gtk_label_new(labelstr);
88
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
89
gtk_container_add(GTK_CONTAINER(box), label);
91
label = gtk_label_new(_("Username:"));
92
gtk_container_add(GTK_CONTAINER(box), label);
94
textuser = boxed_entry_with_text(user, 80, box);
96
label = gtk_label_new(_("Password:"));
97
gtk_container_add(GTK_CONTAINER(box), label);
99
textpasswd = boxed_entry_with_text("", 80, box);
100
gtk_entry_set_visibility(GTK_ENTRY(textpasswd), FALSE);
102
gtk_widget_show_all(dialog);
103
ret = gtk_dialog_run(GTK_DIALOG(dialog));
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);
113
gtk_widget_destroy(dialog);
117
static gchar *make_key(gchar * proto, gchar * server, int port, gchar * object)
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);
125
static void fill_authen_callback(GnomeVFSModuleCallbackFillAuthenticationIn * in, gsize in_size,
126
GnomeVFSModuleCallbackFillAuthenticationOut * out, gsize out_size,
132
DEBUG_MSG("fill_authen_callback: uri = %s\n", in->uri);
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");
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));
145
static void full_authen_callback(GnomeVFSModuleCallbackFullAuthenticationIn * in, gsize in_size,
146
GnomeVFSModuleCallbackFullAuthenticationOut * out, gsize out_size,
152
DEBUG_MSG("full_authen_callback: uri = %s flags = 0x%x\n", in->uri, in->flags);
154
/* key = make_key(in->protocol, in->server, in->port, in->object);
155
g_hash_table_remove(data->hash, key);
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;
165
out->abort_auth = TRUE;
166
DEBUG_MSG("full_authen_callback: user = %s len(passwd)=%d\n", out->username, strlen(out->password));
169
static void save_authen_callback(GnomeVFSModuleCallbackSaveAuthenticationIn * in, gsize in_size,
170
GnomeVFSModuleCallbackSaveAuthenticationOut * out, gsize out_size,
176
DEBUG_MSG("save_authen_callback: uri = %s keyring = %s\n", in->uri, in->keyring);
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);
184
* set_authen_callbacks:
186
* register gnome-vfs callbacks for user authentication handling
189
void set_authen_callbacks(void)
192
AuthenData *data = g_new0(AuthenData,1);
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,
198
gnome_vfs_module_callback_set_default(GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION,
199
(GnomeVFSModuleCallback) full_authen_callback, data,
201
gnome_vfs_module_callback_set_default(GNOME_VFS_MODULE_CALLBACK_SAVE_AUTHENTICATION,
202
(GnomeVFSModuleCallback) save_authen_callback, data,
204
gnome_vfs_module_callback_push (GNOME_VFS_MODULE_CALLBACK_FILL_AUTHENTICATION,
205
(GnomeVFSModuleCallback) fill_authen_callback,
208
gnome_vfs_module_callback_push (GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION,
209
(GnomeVFSModuleCallback) full_authen_callback,
212
gnome_vfs_module_callback_push (GNOME_VFS_MODULE_CALLBACK_SAVE_AUTHENTICATION,
213
(GnomeVFSModuleCallback) save_authen_callback,
216
DEBUG_MSG("set_authen_callbacks: callbacks registered\n");
221
#endif /* HAVE_ATLEAST_GNOMEVFS_2_6 */