Copied remotely
[mono.git] / mcs / class / Mono.Directory.LDAP / Mono.Directory.LDAP / LDAP.cs
1 //
2 // Mono.Directory.LDAP.LDAP
3 //
4 // Author:
5 //    Chris Toshok (toshok@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9 //
10 // Just enough (for now) LDAP support to get System.DirectoryServices
11 // working.
12
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33
34 using System;
35 using System.Runtime.InteropServices;
36
37 namespace Mono.Directory.LDAP 
38 {
39         class TimeVal {
40                 public int tv_sec;
41                 public int tv_usec;
42
43                 public static TimeVal FromTimeSpan (TimeSpan span) {
44                         TimeVal tv = new TimeVal();
45                         long nanoseconds;
46
47                         /* make sure we're dealing with a positive TimeSpan */
48                         span = span.Duration();
49
50                         nanoseconds = span.Ticks * 100;
51
52                         tv.tv_sec = (int)(nanoseconds / 1E+09);
53                         tv.tv_usec = (int)((nanoseconds % 1E+09) / 1000);
54
55                         return tv;
56                 }
57         }
58
59         public enum SearchScope {
60                 Base = 0x0000,
61                 OneLevel = 0x0001,
62                 SubTree = 0x0002
63         }
64
65         public class LDAP {
66
67                 /* Search Scopes */
68                 public LDAP (string uri) {
69                         int rv;
70                         rv = ldap_initialize (out ld, uri);
71                         // FIXME throw something here if ldap_initialize returns an error
72                 }
73
74                 public LDAP (string host, int port) {
75                         ld = ldap_init (host, port);
76                         // FIXME throw something here if ldap_init fails.
77                 }
78
79                 public int BindSimple (string who, string cred) {
80                         return ldap_simple_bind_s (ld, who, cred);
81                 }
82
83                 public int StartTLS () {
84                         // FIXME should expose client/server ctrls
85                         return ldap_start_tls_s (ld, IntPtr.Zero, IntPtr.Zero);
86                 }
87
88                 public int Search (string      base_entry,
89                                    SearchScope scope,
90                                    string      filter,
91                                    string[]    attrs,
92                                    bool        attrsonly,
93                                    TimeSpan    timeOut,
94                                    int         sizeLimit,
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);
100                   IntPtr native_res;
101                   int rv;
102
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);
107
108                   if (native_res != IntPtr.Zero)
109                     res = new LDAPMessage (this, native_res);
110                   else
111                     res = null;
112
113                   return rv;
114                 }
115                                    
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
120                 }
121
122                 public IntPtr NativeLDAP {
123                         get { return ld; }
124                 }
125
126                 [DllImport("ldap")]
127                 static extern IntPtr ldap_init(string host, int port);
128
129                 [DllImport("ldap")]
130                 static extern int ldap_initialize(out IntPtr ld, string uri);
131
132                 [DllImport("ldap")]
133                 static extern int ldap_simple_bind_s(IntPtr ld,
134                                                      string who, string cred);
135
136                 [DllImport("ldap")]
137                 static extern int ldap_start_tls_s (IntPtr ld,
138                                                     IntPtr serverctrls,
139                                                     IntPtr clientctrls);
140
141                 [DllImport("ldap")]
142                 static extern int ldap_search_ext_s (IntPtr     ld,
143                                                      string     base_entry,
144                                                      int        scope,
145                                                      string     filter,
146                                                      string[]   attrs,
147                                                      int        attrsonly,
148                                                      IntPtr     serverctrls,
149                                                      IntPtr     clientctrls,
150                                                      ref TimeVal timeout,
151                                                      int        sizelimit,
152                                                      out IntPtr res);
153
154                 [DllImport("ldap")]
155                 static extern int ldap_unbind_ext_s (IntPtr     ld,
156                                                      IntPtr     serverctrls,
157                                                      IntPtr     clientctrls);
158
159                 IntPtr ld;
160         }
161 }