From e2fd0d5e42e8b0d3e36085dd89aff0880bf20996 Mon Sep 17 00:00:00 2001 From: Jeroen Zwartepoorte Date: Mon, 20 Dec 2004 15:20:58 +0000 Subject: [PATCH] 2004-12-20 Jeroen Zwartepoorte * gnomevfs/Makefile.am: * gnomevfs/ModuleCallback.cs: * gnomevfs/ModuleCallbackAuthentication.cs: * gnomevfs/ModuleCallbackFullAuthentication.cs: * gnomevfs/Vfs.cs: Use a custom VfsException for Result errors. * gnomevfs/VfsException.cs: new custom Exception class. * sample/gnomevfs/Makefile.am: * sample/gnomevfs/TestCallback.cs: Implement custom bindings for the ModuleCallback mechanism. Atm, only the GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION and GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION callbacks are implemented. Also added a test-case using the full authentication callback (tested using the sftp: method). [Partially fixes #70602] svn path=/trunk/gtk-sharp/; revision=37972 --- ChangeLog | 17 ++ gnomevfs/Makefile.am | 4 + gnomevfs/ModuleCallback.cs | 39 +++ gnomevfs/ModuleCallbackAuthentication.cs | 129 +++++++++ gnomevfs/ModuleCallbackFullAuthentication.cs | 288 +++++++++++++++++++ gnomevfs/Vfs.cs | 2 +- gnomevfs/VfsException.cs | 39 +++ sample/gnomevfs/Makefile.am | 5 + sample/gnomevfs/TestCallback.cs | 73 +++++ 9 files changed, 595 insertions(+), 1 deletion(-) create mode 100644 gnomevfs/ModuleCallback.cs create mode 100644 gnomevfs/ModuleCallbackAuthentication.cs create mode 100644 gnomevfs/ModuleCallbackFullAuthentication.cs create mode 100644 gnomevfs/VfsException.cs create mode 100644 sample/gnomevfs/TestCallback.cs diff --git a/ChangeLog b/ChangeLog index 4d5918162..97fa198e0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2004-12-20 Jeroen Zwartepoorte + + * gnomevfs/Makefile.am: + * gnomevfs/ModuleCallback.cs: + * gnomevfs/ModuleCallbackAuthentication.cs: + * gnomevfs/ModuleCallbackFullAuthentication.cs: + * gnomevfs/Vfs.cs: Use a custom VfsException for Result errors. + * gnomevfs/VfsException.cs: new custom Exception class. + * sample/gnomevfs/Makefile.am: + * sample/gnomevfs/TestCallback.cs: + + Implement custom bindings for the ModuleCallback mechanism. Atm, only + the GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION and + GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION callbacks are implemented. + Also added a test-case using the full authentication callback (tested + using the sftp: method). [Partially fixes #70602] + 2004-12-18 Mike Kestner * configure.in : bump version in preparation for 1.9.1 release. diff --git a/gnomevfs/Makefile.am b/gnomevfs/Makefile.am index 6ebcbdac7..c8a4f5656 100644 --- a/gnomevfs/Makefile.am +++ b/gnomevfs/Makefile.am @@ -39,9 +39,13 @@ sources = \ FileInfo.cs \ Handle.cs \ MimeType.cs \ + ModuleCallback.cs \ + ModuleCallbackAuthentication.cs \ + ModuleCallbackFullAuthentication.cs \ Monitor.cs \ Sync.cs \ Vfs.cs \ + VfsException.cs \ VfsStream.cs \ VfsStreamAsyncResult.cs \ Xfer.cs \ diff --git a/gnomevfs/ModuleCallback.cs b/gnomevfs/ModuleCallback.cs new file mode 100644 index 000000000..d6320c320 --- /dev/null +++ b/gnomevfs/ModuleCallback.cs @@ -0,0 +1,39 @@ +// ModuleCallback.cs - GnomeVfsModuleCallback* bindings. +// +// Authors: Jeroen Zwartepoorte +// +// Copyright (c) 2004 Jeroen Zwartepoorte +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of version 2 of the Lesser GNU General +// Public License as published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., 59 Temple Place - Suite 330, +// Boston, MA 02111-1307, USA. + +namespace Gnome.Vfs { + public delegate void ModuleCallbackHandler (ModuleCallback cb); + + abstract public class ModuleCallback { + abstract public event ModuleCallbackHandler Callback; + + abstract public void Push (); + + abstract public void Pop (); + + abstract public void SetDefault (); + + abstract public void PushAsync (); + + abstract public void PopAsync (); + + abstract public void SetDefaultAsync (); + } +} diff --git a/gnomevfs/ModuleCallbackAuthentication.cs b/gnomevfs/ModuleCallbackAuthentication.cs new file mode 100644 index 000000000..55c750082 --- /dev/null +++ b/gnomevfs/ModuleCallbackAuthentication.cs @@ -0,0 +1,129 @@ +// ModuleCallbackAuthentication.cs - GnomeVfsModuleCallback* bindings. +// +// Authors: Jeroen Zwartepoorte +// +// Copyright (c) 2004 Jeroen Zwartepoorte +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of version 2 of the Lesser GNU General +// Public License as published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., 59 Temple Place - Suite 330, +// Boston, MA 02111-1307, USA. + +using System; +using System.Runtime.InteropServices; + +namespace Gnome.Vfs { + public class ModuleCallbackAuthentication : ModuleCallback + { + public enum AuthenticationType { + Basic, + Digest + }; + + [StructLayout(LayoutKind.Sequential)] + internal struct ModuleCallbackAuthenticationIn { + public string Uri; + public string Realm; + public bool PreviousAttemptFailed; + public AuthenticationType authType; + private IntPtr _reserved1; + private IntPtr _reserved2; + } + + [StructLayout(LayoutKind.Sequential)] + internal struct ModuleCallbackAuthenticationOut { + public string Username; + public string Password; + private IntPtr _reserved1; + private IntPtr _reserved2; + } + + private delegate void ModuleCallbackAuthenticationNative (ref ModuleCallbackAuthenticationIn authIn, int inSize, + ref ModuleCallbackAuthenticationOut authOut, int outSize, + IntPtr data); + + private delegate void ModuleCallbackAsyncAuthenticationNative (ref ModuleCallbackAuthenticationIn authIn, int inSize, + ref ModuleCallbackAuthenticationOut authOut, int outSize, + IntPtr data, IntPtr resp, IntPtr resp_data); + + public override event ModuleCallbackHandler Callback; + + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_module_callback_push (string callback_name, ModuleCallbackAuthenticationNative callback, IntPtr data, IntPtr destroy); + + public override void Push () + { + gnome_vfs_module_callback_push ("simple-authentication", + new ModuleCallbackAuthenticationNative (OnNativeCallback), + IntPtr.Zero, IntPtr.Zero); + } + + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_module_callback_pop (string callback_name); + + public override void Pop () + { + gnome_vfs_module_callback_pop ("simple-authentication"); + } + + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_module_callback_set_default (string callback_name, ModuleCallbackAuthenticationNative callback, IntPtr data, IntPtr destroy); + + public override void SetDefault () + { + gnome_vfs_module_callback_set_default ("simple-authentication", + new ModuleCallbackAuthenticationNative (OnNativeCallback), + IntPtr.Zero, IntPtr.Zero); + } + + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_async_module_callback_push (string callback_name, ModuleCallbackAsyncAuthenticationNative callback, IntPtr data, IntPtr destroy); + + public override void PushAsync () + { + gnome_vfs_async_module_callback_push ("simple-authentication", + new ModuleCallbackAsyncAuthenticationNative (OnNativeAsyncCallback), + IntPtr.Zero, IntPtr.Zero); + } + + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_async_module_callback_pop (string callback_name); + + public override void PopAsync () + { + gnome_vfs_async_module_callback_pop ("simple-authentication"); + } + + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_async_module_callback_set_default (string callback_name, ModuleCallbackAsyncAuthenticationNative callback, IntPtr data, IntPtr destroy); + + public override void SetDefaultAsync () + { + gnome_vfs_async_module_callback_set_default ("simple-authentication", + new ModuleCallbackAsyncAuthenticationNative (OnNativeAsyncCallback), + IntPtr.Zero, IntPtr.Zero); + } + + private void OnNativeAsyncCallback (ref ModuleCallbackAuthenticationIn authIn, int inSize, + ref ModuleCallbackAuthenticationOut authOut, int outSize, + IntPtr data, IntPtr resp, IntPtr resp_data) + { + OnNativeCallback (ref authIn, inSize, ref authOut, outSize, data); + } + + private void OnNativeCallback (ref ModuleCallbackAuthenticationIn authIn, int inSize, + ref ModuleCallbackAuthenticationOut authOut, int outSize, IntPtr data) + { + Console.WriteLine ("OnNativeCallback"); + } + } +} diff --git a/gnomevfs/ModuleCallbackFullAuthentication.cs b/gnomevfs/ModuleCallbackFullAuthentication.cs new file mode 100644 index 000000000..438d059e0 --- /dev/null +++ b/gnomevfs/ModuleCallbackFullAuthentication.cs @@ -0,0 +1,288 @@ +// ModuleCallbackFullAuthentication.cs - GnomeVfsModuleCallback* bindings. +// +// Authors: Jeroen Zwartepoorte +// +// Copyright (c) 2004 Jeroen Zwartepoorte +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of version 2 of the Lesser GNU General +// Public License as published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., 59 Temple Place - Suite 330, +// Boston, MA 02111-1307, USA. + +using System; +using System.Runtime.InteropServices; + +namespace Gnome.Vfs { + public class ModuleCallbackFullAuthentication : ModuleCallback + { + [StructLayout(LayoutKind.Sequential)] + internal struct ModuleCallbackFullAuthenticationIn { + public ModuleCallbackFullAuthenticationFlags Flags; + public string Uri; + public string Protocol; + public string Server; + public string Object; + public int Port; + public string Authtype; + public string Username; + public string Domain; + public string DefaultUser; + public string DefaultDomain; + private IntPtr _reserved1; + private IntPtr _reserved2; + } + + [StructLayout(LayoutKind.Sequential)] + internal struct ModuleCallbackFullAuthenticationOut { + public bool AbortAuth; + public string Username; + public string Domain; + public string Password; + public bool SavePassword; + public string Keyring; + private IntPtr _reserved1; + private IntPtr _reserved2; + } + + private delegate void ModuleCallbackFullAuthenticationNative (ref ModuleCallbackFullAuthenticationIn authIn, int inSize, + ref ModuleCallbackFullAuthenticationOut authOut, int outSize, + IntPtr data); + + private delegate void ModuleCallbackAsyncFullAuthenticationNative (ref ModuleCallbackFullAuthenticationIn authIn, int inSize, + ref ModuleCallbackFullAuthenticationOut authOut, int outSize, + IntPtr data, IntPtr resp, IntPtr resp_data); + + // ModuleCallbackFullAuthenticationIn fields. + private ModuleCallbackFullAuthenticationFlags flags; + private string uri; + private string protocol; + private string server; + private string obj; + private int port; + private string authtype; + private string username; + private string domain; + private string defaultUser; + private string defaultDomain; + // ModuleCallbackFullAuthenticationOut fields. + private bool abortAuth; + private string password; + private bool savePassword; + private string keyring; + + public ModuleCallbackFullAuthenticationFlags Flags { + get { + return flags; + } + } + + public string Uri { + get { + return uri; + } + } + + public string Protocol { + get { + return protocol; + } + } + + public string Server { + get { + return server; + } + } + + public string Object { + get { + return obj; + } + } + + public int Port { + get { + return port; + } + } + + public string AuthType { + get { + return authtype; + } + } + + public string Username { + get { + return username; + } + set { + username = value; + } + } + + public string Domain { + get { + return domain; + } + set { + domain = value; + } + } + + public string DefaultUser { + get { + return defaultUser; + } + } + + public string DefaultDomain { + get { + return defaultDomain; + } + } + + public bool AbortAuth { + set { + abortAuth = value; + } + } + + public string Password { + set { + password = value; + } + } + + public bool SavePassword { + set { + savePassword = value; + } + } + + public string Keyring { + set { + keyring = value; + } + } + + public override event ModuleCallbackHandler Callback; + + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_module_callback_push (string callback_name, ModuleCallbackFullAuthenticationNative callback, IntPtr data, IntPtr destroy); + + public override void Push () + { + gnome_vfs_module_callback_push ("full-authentication", + new ModuleCallbackFullAuthenticationNative (OnNativeCallback), + IntPtr.Zero, IntPtr.Zero); + } + + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_module_callback_pop (string callback_name); + + public override void Pop () + { + gnome_vfs_module_callback_pop ("full-authentication"); + } + + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_module_callback_set_default (string callback_name, ModuleCallbackFullAuthenticationNative callback, IntPtr data, IntPtr destroy); + + public override void SetDefault () + { + gnome_vfs_module_callback_set_default ("full-authentication", + new ModuleCallbackFullAuthenticationNative (OnNativeCallback), + IntPtr.Zero, IntPtr.Zero); + } + + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_async_module_callback_push (string callback_name, ModuleCallbackAsyncFullAuthenticationNative callback, IntPtr data, IntPtr destroy); + + public override void PushAsync () + { + gnome_vfs_async_module_callback_push ("full-authentication", + new ModuleCallbackAsyncFullAuthenticationNative (OnNativeAsyncCallback), + IntPtr.Zero, IntPtr.Zero); + } + + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_async_module_callback_pop (string callback_name); + + public override void PopAsync () + { + gnome_vfs_async_module_callback_pop ("full-authentication"); + } + + [DllImport ("gnomevfs-2")] + private static extern void gnome_vfs_async_module_callback_set_default (string callback_name, ModuleCallbackAsyncFullAuthenticationNative callback, IntPtr data, IntPtr destroy); + + public override void SetDefaultAsync () + { + gnome_vfs_async_module_callback_set_default ("full-authentication", + new ModuleCallbackAsyncFullAuthenticationNative (OnNativeAsyncCallback), + IntPtr.Zero, IntPtr.Zero); + } + + private void OnNativeAsyncCallback (ref ModuleCallbackFullAuthenticationIn authIn, int inSize, + ref ModuleCallbackFullAuthenticationOut authOut, int outSize, + IntPtr data, IntPtr resp, IntPtr resp_data) + { + OnNativeCallback (ref authIn, inSize, ref authOut, outSize, data); + } + + private void OnNativeCallback (ref ModuleCallbackFullAuthenticationIn authIn, int inSize, + ref ModuleCallbackFullAuthenticationOut authOut, int outSize, IntPtr data) + { + // Copy the authIn fields. + flags = authIn.Flags; + uri = authIn.Uri; + protocol = authIn.Protocol; + server = authIn.Server; + obj = authIn.Object; + port = authIn.Port; + authtype = authIn.Authtype; + username = authIn.Username; + domain = authIn.Domain; + defaultUser = authIn.DefaultUser; + defaultDomain = authIn.DefaultDomain; + + // Activate the callback. + ModuleCallbackHandler handler = Callback; + if (handler != null) { + handler (this); + // Copy the values back to the authOut. + authOut.AbortAuth = abortAuth; + authOut.Username = username; + authOut.Domain = domain; + authOut.Password = password; + authOut.SavePassword = savePassword; + authOut.Keyring = keyring; + } + } + + private void DumpAuthIn (ref ModuleCallbackFullAuthenticationIn authIn) + { + Console.WriteLine ("Flags: {0}", authIn.Flags); + Console.WriteLine ("Uri: {0}", authIn.Uri); + Console.WriteLine ("Protocol: {0}", authIn.Protocol); + Console.WriteLine ("Server: {0}", authIn.Server); + Console.WriteLine ("Object: {0}", authIn.Object); + Console.WriteLine ("Port: {0}", authIn.Port); + Console.WriteLine ("Authtype: {0}", authIn.Authtype); + Console.WriteLine ("Username: {0}", authIn.Username); + Console.WriteLine ("Domain: {0}", authIn.Domain); + Console.WriteLine ("DefaultUser: {0}", authIn.DefaultUser); + Console.WriteLine ("DefaultDomain: {0}", authIn.DefaultDomain); + } + } +} diff --git a/gnomevfs/Vfs.cs b/gnomevfs/Vfs.cs index 88dd38422..33811cfdd 100644 --- a/gnomevfs/Vfs.cs +++ b/gnomevfs/Vfs.cs @@ -122,7 +122,7 @@ namespace Gnome.Vfs { case Result.ErrorNotFound: throw new FileNotFoundException (uri); default: - throw new IOException (ResultToString (result)); + throw new VfsException (result); } } } diff --git a/gnomevfs/VfsException.cs b/gnomevfs/VfsException.cs new file mode 100644 index 000000000..2b11321aa --- /dev/null +++ b/gnomevfs/VfsException.cs @@ -0,0 +1,39 @@ +// VfsException.cs - VfsException class encapsulating Result. +// +// Authors: Jeroen Zwartepoorte +// +// Copyright (c) 2004 Jeroen Zwartepoorte +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of version 2 of the Lesser GNU General +// Public License as published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., 59 Temple Place - Suite 330, +// Boston, MA 02111-1307, USA. + +using System; + +namespace Gnome.Vfs { + public class VfsException : Exception { + private Result result; + + public VfsException (Result result) + : base (Vfs.ResultToString (result)) + { + this.result = result; + } + + public Result Result { + get { + return result; + } + } + } +} diff --git a/sample/gnomevfs/Makefile.am b/sample/gnomevfs/Makefile.am index eb0a77d99..a4ffb6853 100644 --- a/sample/gnomevfs/Makefile.am +++ b/sample/gnomevfs/Makefile.am @@ -1,6 +1,7 @@ TARGETS = \ TestAsync.exe \ TestAsyncStream.exe \ + TestCallback.exe \ TestDirectory.exe \ TestInfo.exe \ TestMime.exe \ @@ -22,6 +23,7 @@ CLEANFILES = $(TARGETS) EXTRA_DIST = \ TestAsync.cs \ TestAsyncStream.cs \ + TestCallback.cs \ TestDirectory.cs \ TestInfo.cs \ TestMime.cs \ @@ -40,6 +42,9 @@ TestAsync.exe: $(srcdir)/TestAsync.cs $(assemblies) TestAsyncStream.exe: $(srcdir)/TestAsyncStream.cs $(assemblies) $(CSC) /out:TestAsyncStream.exe $(references) $(srcdir)/TestAsyncStream.cs +TestCallback.exe: $(srcdir)/TestCallback.cs $(assemblies) + $(CSC) /out:TestCallback.exe $(references) $(srcdir)/TestCallback.cs + TestDirectory.exe: $(srcdir)/TestDirectory.cs $(assemblies) $(CSC) /out:TestDirectory.exe $(references) $(srcdir)/TestDirectory.cs diff --git a/sample/gnomevfs/TestCallback.cs b/sample/gnomevfs/TestCallback.cs new file mode 100644 index 000000000..7cec3933c --- /dev/null +++ b/sample/gnomevfs/TestCallback.cs @@ -0,0 +1,73 @@ +using GLib; +using Gnome.Vfs; +using System; +using System.Text; + +namespace Test.Gnome.Vfs { + public class TestCallback { + private static MainLoop loop; + + static void Main (string[] args) + { + if (args.Length != 1) { + Console.WriteLine ("Usage: TestCallback "); + return; + } + + Gnome.Vfs.Vfs.Initialize (); + + Gnome.Vfs.Uri uri = new Gnome.Vfs.Uri (args[0]); + Handle handle; + + // Test 1: Attempt to access a URI requiring authentication w/o a callback registered. + try { + handle = Sync.Open (uri, OpenMode.Read); + Sync.Close (handle); + Console.WriteLine ("Uri '{0}' doesn't require authentication", uri); + return; + } catch (VfsException ex) { + if (ex.Result != Result.ErrorAccessDenied) + throw ex; + } + + // Test 2: Attempt an open that requires authentication. + ModuleCallbackFullAuthentication cb = new ModuleCallbackFullAuthentication (); + cb.Callback += new ModuleCallbackHandler (OnAuthenticate); + cb.SetDefault (); + + handle = Sync.Open (uri, OpenMode.Read); + Sync.Close (handle); + + // Test 3: This call should not require any new authentication. + Console.WriteLine ("File info: \n{0}", uri.GetFileInfo ()); + + // Test 4: Attempt a call to the parent uri. + FileInfo[] entries = Directory.GetEntries (uri.Parent); + Console.WriteLine ("Directory '{0}' has {1} entries", uri.Parent, entries.Length); + + // Test 5: Pop the authentication callback and try again. + cb.Pop (); + try { + handle = Sync.Open (uri, OpenMode.Read); + } catch (VfsException ex) { + if (ex.Result != Result.ErrorAccessDenied) + throw ex; + } + + Gnome.Vfs.Vfs.Shutdown (); + } + + private static void OnAuthenticate (ModuleCallback cb) + { + ModuleCallbackFullAuthentication fcb = cb as ModuleCallbackFullAuthentication; + Console.Write ("Enter your username ({0}): ", fcb.Username); + string username = Console.ReadLine (); + Console.Write ("Enter your password : "); + string passwd = Console.ReadLine (); + + if (username.Length > 0) + fcb.Username = username; + fcb.Password = passwd; + } + } +}