2 // Mono.Directory.LDAP.LDAP
5 // Chris Toshok (toshok@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
10 // Just enough (for now) LDAP support to get System.DirectoryServices
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System
.Runtime
.InteropServices
;
37 namespace Mono
.Directory
.LDAP
43 public static TimeVal
FromTimeSpan (TimeSpan span
) {
44 TimeVal tv
= new TimeVal();
47 /* make sure we're dealing with a positive TimeSpan */
48 span
= span
.Duration();
50 nanoseconds
= span
.Ticks
* 100;
52 tv
.tv_sec
= (int)(nanoseconds
/ 1E+09);
53 tv
.tv_usec
= (int)((nanoseconds
% 1E+09) / 1000);
59 public enum SearchScope
{
68 public LDAP (string uri
) {
70 rv
= ldap_initialize (out ld
, uri
);
71 // FIXME throw something here if ldap_initialize returns an error
74 public LDAP (string host
, int port
) {
75 ld
= ldap_init (host
, port
);
76 // FIXME throw something here if ldap_init fails.
79 public int BindSimple (string who
, string cred
) {
80 return ldap_simple_bind_s (ld
, who
, cred
);
83 public int StartTLS () {
84 // FIXME should expose client/server ctrls
85 return ldap_start_tls_s (ld
, IntPtr
.Zero
, IntPtr
.Zero
);
88 public int Search (string base_entry
,
95 out LDAPMessage res
) {
96 // FIXME should expose client/server ctrls
97 IntPtr serverctrls
= new IntPtr();
98 IntPtr clientctrls
= new IntPtr();
99 TimeVal tv
= TimeVal
.FromTimeSpan (timeOut
);
103 rv
= ldap_search_ext_s (ld
, base_entry
, (int) scope
, filter
,
104 attrs
, attrsonly
? 1 : 0,
105 serverctrls
, clientctrls
,
106 ref tv
, sizeLimit
, out native_res
);
108 if (native_res
!= IntPtr
.Zero
)
109 res
= new LDAPMessage (this, native_res
);
116 public void Unbind () {
117 // FIXME should expose client/server ctrls
118 ldap_unbind_ext_s (ld
, IntPtr
.Zero
, IntPtr
.Zero
);
119 // FIXME throw something here if ldap_unbind_ext_s returns an error
122 public IntPtr NativeLDAP
{
127 static extern IntPtr
ldap_init(string host
, int port
);
130 static extern int ldap_initialize(out IntPtr ld
, string uri
);
133 static extern int ldap_simple_bind_s(IntPtr ld
,
134 string who
, string cred
);
137 static extern int ldap_start_tls_s (IntPtr ld
,
142 static extern int ldap_search_ext_s (IntPtr ld
,
155 static extern int ldap_unbind_ext_s (IntPtr ld
,