From 9749a97dc5c0277a6ebb5b219a4158127d6fcb92 Mon Sep 17 00:00:00 2001 From: Chris Toshok Date: Mon, 16 Sep 2002 21:26:14 +0000 Subject: initial import svn path=/trunk/mcs/; revision=7525 --- mcs/class/Mono.Directory.LDAP/ChangeLog | 0 .../Mono.Directory.LDAP/LDAP.cs | 141 +++++++++++++++++++++ .../Mono.Directory.LDAP/LDAPMessage.cs | 134 ++++++++++++++++++++ .../Test/Mono.Directory.LDAP/AllTests.cs | 30 +++++ .../Test/Mono.Directory.LDAP/BindSimpleTest.cs | 43 +++++++ .../Test/Mono.Directory.LDAP/QueryRootDSE.cs | 70 ++++++++++ mcs/class/Mono.Directory.LDAP/Test/NUnit.Prefs | 0 .../Mono.Directory.LDAP/Test/dslib_linux_test.args | 10 ++ mcs/class/Mono.Directory.LDAP/Test/makefile.gnu | 23 ++++ mcs/class/Mono.Directory.LDAP/list | 2 + mcs/class/Mono.Directory.LDAP/makefile.gnu | 14 ++ 11 files changed, 467 insertions(+) create mode 100644 mcs/class/Mono.Directory.LDAP/ChangeLog create mode 100644 mcs/class/Mono.Directory.LDAP/Mono.Directory.LDAP/LDAP.cs create mode 100644 mcs/class/Mono.Directory.LDAP/Mono.Directory.LDAP/LDAPMessage.cs create mode 100644 mcs/class/Mono.Directory.LDAP/Test/Mono.Directory.LDAP/AllTests.cs create mode 100644 mcs/class/Mono.Directory.LDAP/Test/Mono.Directory.LDAP/BindSimpleTest.cs create mode 100644 mcs/class/Mono.Directory.LDAP/Test/Mono.Directory.LDAP/QueryRootDSE.cs create mode 100644 mcs/class/Mono.Directory.LDAP/Test/NUnit.Prefs create mode 100644 mcs/class/Mono.Directory.LDAP/Test/dslib_linux_test.args create mode 100644 mcs/class/Mono.Directory.LDAP/Test/makefile.gnu create mode 100644 mcs/class/Mono.Directory.LDAP/list create mode 100644 mcs/class/Mono.Directory.LDAP/makefile.gnu (limited to 'mcs/class/Mono.Directory.LDAP') diff --git a/mcs/class/Mono.Directory.LDAP/ChangeLog b/mcs/class/Mono.Directory.LDAP/ChangeLog new file mode 100644 index 00000000000..e69de29bb2d diff --git a/mcs/class/Mono.Directory.LDAP/Mono.Directory.LDAP/LDAP.cs b/mcs/class/Mono.Directory.LDAP/Mono.Directory.LDAP/LDAP.cs new file mode 100644 index 00000000000..4fe55f52c57 --- /dev/null +++ b/mcs/class/Mono.Directory.LDAP/Mono.Directory.LDAP/LDAP.cs @@ -0,0 +1,141 @@ +// +// Mono.Directory.LDAP.LDAP +// +// Author: +// Chris Toshok (toshok@ximian.com) +// +// (C) Ximian, Inc. http://www.ximian.com +// +// +// Just enough (for now) LDAP support to get System.DirectoryServices +// working. + +using System; +using System.Runtime.InteropServices; + +namespace Mono.Directory.LDAP +{ + class TimeVal { + public int tv_sec; + public int tv_usec; + + public static TimeVal FromTimeSpan (TimeSpan span) { + TimeVal tv = new TimeVal(); + long nanoseconds; + + /* make sure we're dealing with a positive TimeSpan */ + span = span.Duration(); + + nanoseconds = span.Ticks * 100; + + tv.tv_sec = (int)(nanoseconds / 1E+09); + tv.tv_usec = (int)((nanoseconds % 1E+09) / 1000); + + return tv; + } + } + + public enum SearchScope { + Base = 0x0000, + OneLevel = 0x0001, + SubTree = 0x0002 + } + + public class LDAP { + + /* Search Scopes */ + public LDAP (string uri) { + int rv; + rv = ldap_initialize (out ld, uri); + // FIXME throw something here if ldap_initialize returns an error + } + + public LDAP (string host, int port) { + ld = ldap_init (host, port); + // FIXME throw something here if ldap_init fails. + } + + public int BindSimple (string who, string cred) { + return ldap_simple_bind_s (ld, who, cred); + } + + public int StartTLS () { + // FIXME should expose client/server ctrls + return ldap_start_tls_s (ld, (IntPtr)null, (IntPtr)null); + } + + public int Search (string base_entry, + SearchScope scope, + string filter, + string[] attrs, + bool attrsonly, + TimeSpan timeOut, + int sizeLimit, + out LDAPMessage res) { + // FIXME should expose client/server ctrls + IntPtr serverctrls = new IntPtr(); + IntPtr clientctrls = new IntPtr(); + TimeVal tv = TimeVal.FromTimeSpan (timeOut); + IntPtr native_res; + int rv; + + rv = ldap_search_ext_s (ld, base_entry, (int) scope, filter, + attrs, attrsonly ? 1 : 0, + serverctrls, clientctrls, + ref tv, sizeLimit, out native_res); + + if (native_res != IntPtr.Zero) + res = new LDAPMessage (this, native_res); + else + res = null; + + return rv; + } + + + public void Unbind () { + // FIXME should expose client/server ctrls + ldap_unbind_ext_s (ld, (IntPtr)null, (IntPtr)null); + // FIXME throw something here if ldap_unbind_ext_s returns an error + } + + public IntPtr NativeLDAP { + get { return ld; } + } + + [DllImport("ldap")] + static extern IntPtr ldap_init(string host, int port); + + [DllImport("ldap")] + static extern int ldap_initialize(out IntPtr ld, string uri); + + [DllImport("ldap")] + static extern int ldap_simple_bind_s(IntPtr ld, + string who, string cred); + + [DllImport("ldap")] + static extern int ldap_start_tls_s (IntPtr ld, + IntPtr serverctrls, + IntPtr clientctrls); + + [DllImport("ldap")] + static extern int ldap_search_ext_s (IntPtr ld, + string base_entry, + int scope, + string filter, + string[] attrs, + int attrsonly, + IntPtr serverctrls, + IntPtr clientctrls, + ref TimeVal timeout, + int sizelimit, + out IntPtr res); + + [DllImport("ldap")] + static extern int ldap_unbind_ext_s (IntPtr ld, + IntPtr serverctrls, + IntPtr clientctrls); + + IntPtr ld; + } +} diff --git a/mcs/class/Mono.Directory.LDAP/Mono.Directory.LDAP/LDAPMessage.cs b/mcs/class/Mono.Directory.LDAP/Mono.Directory.LDAP/LDAPMessage.cs new file mode 100644 index 00000000000..8e99c4102e4 --- /dev/null +++ b/mcs/class/Mono.Directory.LDAP/Mono.Directory.LDAP/LDAPMessage.cs @@ -0,0 +1,134 @@ +// +// Mono.Directory.LDAP.LDAPMessage +// +// Author: +// Chris Toshok (toshok@ximian.com) +// +// (C) Ximian, Inc. http://www.ximian.com +// +// +// Just enough (for now) LDAP support to get System.DirectoryServices +// working. + +using System; +using System.Runtime.InteropServices; + +namespace Mono.Directory.LDAP +{ + public class LDAPMessage { + + internal LDAPMessage(LDAP ld, IntPtr ldm) { + this.ld = ld; + this.ldm = ldm; + } + + public LDAPMessage FirstMessage () { + IntPtr nm = ldap_first_message (ld.NativeLDAP ,ldm); + + if (nm == IntPtr.Zero) + return null; + else + return new LDAPMessage (ld, nm); + } + + public LDAPMessage NextMessage () { + IntPtr nm = ldap_next_message (ld.NativeLDAP ,ldm); + + if (nm == IntPtr.Zero) + return null; + else + return new LDAPMessage (ld, nm); + } + + public int CountMessages () { + return ldap_count_messages (ld.NativeLDAP, ldm); + } + + public LDAPMessage FirstEntry() { + IntPtr nm = ldap_first_entry (ld.NativeLDAP ,ldm); + + if (nm == IntPtr.Zero) + return null; + else + return new LDAPMessage (ld, nm); + } + + public LDAPMessage NextEntry() { + IntPtr nm = ldap_next_entry (ld.NativeLDAP ,ldm); + + if (nm == IntPtr.Zero) + return null; + else + return new LDAPMessage (ld, nm); + } + + public int CountEntries() { + return ldap_count_entries (ld.NativeLDAP, ldm); + } + + public string DN { + get { return ldap_get_dn (ld.NativeLDAP, ldm); } + } + + [MonoTODO] + public string[] GetValues (string target) { + throw new NotImplementedException (); + /* + string[] ldap_values; + + Console.WriteLine ("calling ldap_get_values ({0})", target); + + ldap_values = ldap_get_values (ld.NativeLDAP, ldm, target); + + if (ldap_values != null) { + string[] rv; + int i; + + rv = new string[ldap_values.Length - 1]; + for (i = 0; i < ldap_values.Length - 1; i ++) + rv[i] = ldap_values[i]; + + return rv; + } + else { + return null; + } + */ + } + + [DllImport("ldap")] + extern static string ldap_get_dn (IntPtr ld, IntPtr ldm); + + [DllImport("ldap")] + extern static IntPtr ldap_first_message (IntPtr ld, IntPtr ldm); + + [DllImport("ldap")] + extern static IntPtr ldap_next_message (IntPtr ld, IntPtr ldm); + + [DllImport("ldap")] + extern static int ldap_count_messages (IntPtr ld, IntPtr ldm); + + [DllImport("ldap")] + extern static IntPtr ldap_first_entry (IntPtr ld, IntPtr ldm); + + [DllImport("ldap")] + extern static IntPtr ldap_next_entry (IntPtr ld, IntPtr ldm); + + [DllImport("ldap")] + extern static int ldap_count_entries (IntPtr ld, IntPtr ldm); + + [DllImport("ldap")] + extern static string ldap_first_attribute (IntPtr ld, IntPtr ldm, out IntPtr ber); + + [DllImport("ldap")] + extern static string ldap_next_attribute (IntPtr ld, IntPtr ldm, IntPtr ber); + + /* + [DllImport("ldapglue")] + extern static void ldapsharp_get_values (IntPtr ld, IntPtr ldm, string target, + out string[] values, out int count); + */ + IntPtr ldm; + LDAP ld; + } +} diff --git a/mcs/class/Mono.Directory.LDAP/Test/Mono.Directory.LDAP/AllTests.cs b/mcs/class/Mono.Directory.LDAP/Test/Mono.Directory.LDAP/AllTests.cs new file mode 100644 index 00000000000..678d57d296a --- /dev/null +++ b/mcs/class/Mono.Directory.LDAP/Test/Mono.Directory.LDAP/AllTests.cs @@ -0,0 +1,30 @@ +// +// MonoTests.System.DirectoryServices.AllTests, System.DirectoryServices.dll +// +// Author: +// Chris Toshok +// + +using NUnit.Framework; +using System; +using MonoTests.Directory.LDAP; + +namespace MonoTests.Directory.LDAP { + + public class AllTests : TestCase { + + public AllTests (string name) : base (name) + { + } + + public static ITest Suite { + get { + TestSuite suite = new TestSuite (); + suite.AddTest (BindSimpleTest.Suite); + suite.AddTest (QueryRootDSE.Suite); + return suite; + } + } + } +} + diff --git a/mcs/class/Mono.Directory.LDAP/Test/Mono.Directory.LDAP/BindSimpleTest.cs b/mcs/class/Mono.Directory.LDAP/Test/Mono.Directory.LDAP/BindSimpleTest.cs new file mode 100644 index 00000000000..c184d668603 --- /dev/null +++ b/mcs/class/Mono.Directory.LDAP/Test/Mono.Directory.LDAP/BindSimpleTest.cs @@ -0,0 +1,43 @@ + +using NUnit.Framework; +using System; +using Mono.Directory.LDAP; + +namespace MonoTests.Directory.LDAP +{ + public class BindSimpleTest : TestCase { + public BindSimpleTest () : + base ("[MonoTests.Directory.LDAP.BindSimpleTest]'") {} + + public BindSimpleTest (string name) : + base (name) {} + + protected override void SetUp () {} + + protected override void TearDown () {} + + public static ITest Suite + { + get { + return new TestSuite (typeof (BindSimpleTest)); + } + } + + + public void TestStuff() { + string myLDAPPath = "ldap://ldap.toshok.org"; + string username = "cn=Manager,dc=toshok,dc=org", passwd = "evotest"; + try { + LDAP ld = new LDAP (myLDAPPath); + + ld.BindSimple (username, passwd); + + Console.WriteLine("Successfully bound {0} at {1}", username, myLDAPPath); + } + catch(Exception e) { + Console.WriteLine("The '" + myLDAPPath + "' path not found."); + Console.WriteLine("Exception : " + e.Message); + } + } + } +} diff --git a/mcs/class/Mono.Directory.LDAP/Test/Mono.Directory.LDAP/QueryRootDSE.cs b/mcs/class/Mono.Directory.LDAP/Test/Mono.Directory.LDAP/QueryRootDSE.cs new file mode 100644 index 00000000000..68b77163b8a --- /dev/null +++ b/mcs/class/Mono.Directory.LDAP/Test/Mono.Directory.LDAP/QueryRootDSE.cs @@ -0,0 +1,70 @@ + +using NUnit.Framework; +using System; +using Mono.Directory.LDAP; + +namespace MonoTests.Directory.LDAP +{ + public class QueryRootDSE : TestCase { + public QueryRootDSE () : + base ("[MonoTests.Directory.LDAP.QueryRootDSE]'") {} + + public QueryRootDSE (string name) : + base (name) {} + + protected override void SetUp () {} + + protected override void TearDown () {} + + public static ITest Suite + { + get { + return new TestSuite (typeof (QueryRootDSE)); + } + } + + + public void TestStuff() { + string myLDAPPath = "ldap://ldap.toshok.org"; + try { + LDAP ld = new LDAP (myLDAPPath); + LDAPMessage res, entry; + string[] attrs = { "+", null }; + + /* don't bind, we do this anonymously */ + + ld.Search ("" /* root dse */, + SearchScope.Base, + "(objectclass=*)", + attrs, false, + TimeSpan.FromSeconds(10), 0 /* no size limit */, + out res); + + if (res == null) { + Console.WriteLine ("the search failed"); + } + + Console.WriteLine ("There are {0} entries", res.CountEntries()); + + entry = res.FirstEntry(); + if (entry == null) + Console.WriteLine ("null returned from res.FirstEntry"); + + string[] extensions = entry.GetValues ("supportedExtension"); + + if (extensions != null) { + foreach( String e in extensions ) + Console.WriteLine ("Supported Extension: {0}\n", e); + } + else { + Console.WriteLine ("null returned from entry.GetValues\n"); + } + } + catch(Exception e) { + Console.WriteLine("The '" + myLDAPPath + "' path not found."); + Console.WriteLine("Exception : " + e.Message); + Console.WriteLine(e.StackTrace); + } + } + } +} diff --git a/mcs/class/Mono.Directory.LDAP/Test/NUnit.Prefs b/mcs/class/Mono.Directory.LDAP/Test/NUnit.Prefs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/mcs/class/Mono.Directory.LDAP/Test/dslib_linux_test.args b/mcs/class/Mono.Directory.LDAP/Test/dslib_linux_test.args new file mode 100644 index 00000000000..f6fbe0f7853 --- /dev/null +++ b/mcs/class/Mono.Directory.LDAP/Test/dslib_linux_test.args @@ -0,0 +1,10 @@ +--target library +-o dslib_linux_test.dll +--noconfig +-r ../../lib/Mono.Directory.LDAP.dll +-r ../../lib/corlib.dll +-r ../../lib/System.dll +-r ../../../nunit/NUnitCore_mono.dll +Mono.Directory.LDAP/BindSimpleTest.cs +Mono.Directory.LDAP/QueryRootDSE.cs +Mono.Directory.LDAP/AllTests.cs diff --git a/mcs/class/Mono.Directory.LDAP/Test/makefile.gnu b/mcs/class/Mono.Directory.LDAP/Test/makefile.gnu new file mode 100644 index 00000000000..943289f4cb7 --- /dev/null +++ b/mcs/class/Mono.Directory.LDAP/Test/makefile.gnu @@ -0,0 +1,23 @@ +topdir = ../../.. + +LIBRARY = dslib_linux_test.dll + +LIB_LIST = dslib_linux_test.args +LIB_FLAGS = -r ../../lib/Mono.Directory.LDAP.dll -r ../../lib/corlib.dll -r ../../lib/System.dll \ + -r $(topdir)/nunit/src/NUnitCore/NUnitCore_mono.dll + +include ../../library.make + +MCS_FLAGS = --target library --noconfig + +TEST_SUITE_PREFIX = MonoTests.Directory.LDAP. +TEST_SUITE = AllTests +NUNITCONSOLE=$(topdir)/nunit/src/NUnitConsole/NUnitConsole_mono.exe +NUNIT_MONO_PATH=$(topdir)/nunit/src/NUnitCore:. + +test: $(LIBRARY) run_test + +.PHONY: run_test + +run_test: + MONO_PATH=$(NUNIT_MONO_PATH) mono $(NUNITCONSOLE) $(TEST_SUITE_PREFIX)$(TEST_SUITE),dslib_linux_test.dll diff --git a/mcs/class/Mono.Directory.LDAP/list b/mcs/class/Mono.Directory.LDAP/list new file mode 100644 index 00000000000..fffdb42294d --- /dev/null +++ b/mcs/class/Mono.Directory.LDAP/list @@ -0,0 +1,2 @@ +Mono.Directory.LDAP/LDAP.cs +Mono.Directory.LDAP/LDAPMessage.cs diff --git a/mcs/class/Mono.Directory.LDAP/makefile.gnu b/mcs/class/Mono.Directory.LDAP/makefile.gnu new file mode 100644 index 00000000000..6ca9c4697dc --- /dev/null +++ b/mcs/class/Mono.Directory.LDAP/makefile.gnu @@ -0,0 +1,14 @@ +topdir = ../.. + +LIBRARY = ../lib/Mono.Directory.LDAP.dll + +LIB_LIST = list +LIB_FLAGS = -r corlib -r System -r System.Data -r mscorlib + +SOURCES_INCLUDE=*.cs +SOURCES_EXCLUDE=\ + ./Test* + +export MONO_PATH_PREFIX = ../lib: + +include ../library.make -- cgit v1.2.3