This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.DirectoryServices / System.DirectoryServices / DirectoryEntries.cs
1 /******************************************************************************
2 * The MIT License
3 * Copyright (c) 2003 Novell Inc.,  www.novell.com
4
5 * Permission is hereby granted, free of charge, to any person obtaining  a copy
6 * of this software and associated documentation files (the Software), to deal
7 * in the Software without restriction, including  without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
9 * copies of the Software, and to  permit persons to whom the Software is 
10 * furnished to do so, subject to the following conditions:
11
12 * The above copyright notice and this permission notice shall be included in 
13 * all copies or substantial portions of the Software.
14
15 * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *******************************************************************************/
23
24 //
25 // System.DirectoryServices.DirectoryEntries.cs
26 //
27 // Author:
28 //   Sunil Kumar (sunilk@novell.com)
29 //
30 // (C) Novell Inc.
31 //
32
33 using System.Collections;
34 using Novell.Directory.Ldap;
35
36 namespace System.DirectoryServices
37 {
38         
39         /// <summary>
40         ///Contains the children (child entries) of an entry in
41         /// a Ldap Directory
42         /// </summary>
43         public class DirectoryEntries : IEnumerable
44         {
45                 private LdapConnection _Conn=null;
46                 private string _Bpath=null;
47                 private string _Buser=null;
48                 private string _Bpass=null;
49                 private string _Basedn=null;
50                 private ArrayList m_oValues=null;
51
52
53                 /// <summary> Initializes the Connection and other properties.
54                 /// 
55                 /// </summary>
56                 private void InitBlock()
57                 {
58                         try                     {
59                                 LdapUrl lUrl=new LdapUrl(_Bpath);
60                                 _Conn = new LdapConnection();
61                                 _Conn.Connect(lUrl.Host,lUrl.Port);
62                                 _Conn.Bind(_Buser,_Bpass);
63                         }
64                         catch(LdapException ex)                 {
65                                 Console.WriteLine("Error:" + ex.LdapErrorMessage);
66                                 throw ex;
67                         }
68                         catch(Exception e)                              {
69                                 Console.WriteLine("Error:" +  e.Message);
70                                 throw e;
71                         }
72                 }
73
74                 internal string Basedn
75                 {
76                         get                                                                             {
77                                 if( _Basedn == null)                            {
78 //                                      Console.WriteLine("Basepath:" + _Bpath);
79                                         LdapUrl lurl=new LdapUrl(_Bpath);
80                                         string bdn = lurl.getDN();
81                                         if( bdn != null)
82                                                 _Basedn = bdn;
83                                         else
84                                                 _Basedn = "";
85                                 }
86                                 return _Basedn;
87                         }
88                 }
89                                 
90                 /// <summary> Contains the Path of the Container under which
91                 /// the entries belongs to.
92                 /// </summary>
93                 internal string Bpath
94                 {
95                         get                     {
96                                 return _Bpath;
97                         }
98                         set                     {
99                                 _Bpath=value;
100                         }
101                 }
102
103                 /// <summary> Returns the connection object used to communicate with
104                 /// Ldap server
105                 /// </summary>
106                 internal LdapConnection Conn
107                 {
108                         get                                             {
109                                 if( _Conn == null)      {
110                                         InitBlock();
111                                 }
112                                 return _Conn;
113                         }
114                         set                                             {
115                                 _Conn=value;
116                         }
117                 }
118
119                 /// <summary> Constructs a collection of all the child entries of
120                 /// an entry
121                 /// </summary>
122                 /// <param name="path"> Path of the entry
123                 /// </param>
124                 /// <param name="uname"> Username to Bind as while authenticating to ldap
125                 /// server</param>
126                 /// <param name="passwd"> Password of the user</param>
127                 internal DirectoryEntries(string path, string uname, string passwd)
128                 {
129                         _Bpath = path;
130                         _Buser = uname;
131                         _Bpass = passwd;
132                 }
133
134                 /// <summary> Constructs a collection of all the child entries of
135                 /// a entry
136                 /// </summary>
137                 /// <param name="path"> Path of the entry
138                 /// </param>
139                 /// <param name="lc"> connection object used to connect to ldap server
140                 /// </param>
141                 internal DirectoryEntries(string path,  LdapConnection lc)
142                 {
143                         _Bpath = path;
144                         _Conn = lc;
145                 }
146
147                 public SchemaNameCollection SchemaFilter {
148                         [MonoTODO]
149                         get { throw new NotImplementedException ("System.DirectoryServices.DirectoryEntries.SchemaFilter"); }
150                 }
151
152                 public  IEnumerator GetEnumerator()
153                 {
154                         m_oValues= new ArrayList();
155                         string[] attrs={"objectClass"};
156 //                      Console.WriteLine("BaseDN is:" + Basedn);
157                         LdapSearchResults lsc= Conn.Search(     Basedn,
158                                                                                                 LdapConnection.SCOPE_ONE,
159                                                                                                 "objectClass=*",
160                                                                                                 attrs,
161                                                                                                 false);
162
163                         LdapUrl Burl=new LdapUrl(_Bpath);
164                         string host=Burl.Host;
165                         int port=Burl.Port;
166
167                         while (lsc.hasMore())                   {
168                                 LdapEntry nextEntry = null;
169                                 try                                     {
170                                         nextEntry = lsc.next();
171                                 }
172                                 catch(LdapException e)          {
173                                         Console.WriteLine("Error: " + e.LdapErrorMessage);
174                                         // Exception is thrown, go for next entry
175                                         continue;
176                                 }
177                                 DirectoryEntry dEntry=new DirectoryEntry(Conn);
178                                 string eFdn=nextEntry.DN;
179                                 LdapUrl curl=new LdapUrl(host,port,eFdn);
180                                 dEntry.Path=curl.ToString();
181                                 m_oValues.Add((DirectoryEntry) dEntry);
182                         }
183                         return m_oValues.GetEnumerator();
184                 }
185
186                 /// <summary> Creates a request to create a new entry in the container.
187                 /// 
188                 /// </summary>
189                 /// <param name="name"> RDN of the entry to be created
190                 /// </param>
191                 /// <param name="schemaClassName"> StructuralClassName of the entry to be
192                 /// created.
193                 /// </param>
194                 public DirectoryEntry Add(      string name,string schemaClassName)
195                 {
196                         DirectoryEntry ent=new DirectoryEntry(Conn);
197                         LdapUrl Burl=new LdapUrl(_Bpath);
198                         string eFdn=name+","+Burl.getDN();
199                         LdapUrl curl=new LdapUrl(Burl.Host,Burl.Port,eFdn);
200                         ent.Path=curl.ToString();
201                         ent.Nflag = true;
202                         ent.Properties["objectclass"].Add(schemaClassName);
203                         return ent;
204                 }
205
206                 /// <summary>
207                 /// Deletes a child DirectoryEntry from this collection
208                 /// </summary>
209                 /// <param name="entry">The DirectoryEntry to delete</param>
210                 public void Remove(     DirectoryEntry entry )
211                 {
212                         LdapUrl Burl=new LdapUrl(_Bpath);
213                         string eFDN = entry.Name + "," + Burl.getDN();
214                         Conn.Delete( eFDN);
215                 }
216
217                 /// <summary>
218                 /// Returns the child with the specified name.
219                 /// </summary>
220                 /// <param name="filter">relative distinguised name of the child
221                 /// </param>
222                 /// <returns>Child entry with the specified name </returns>
223                 public DirectoryEntry Find(string filter)
224                 {
225                         DirectoryEntry child=CheckEntry(filter);
226                         return child;
227                 }
228
229                 /// <summary>
230                 /// Returns the child with the specified name and of the specified type.
231                 /// </summary>
232                 /// <param name="filter">relative distinguised name of the child
233                 /// </param>
234                 /// <param name="otype"> Type of the child i.e strutcuralObjectClass
235                 /// name of the child </param>
236                 /// <returns>Child entry with the specified name and type</returns>
237                 public DirectoryEntry Find(string filter, string otype)
238                 {
239                         DirectoryEntry child=CheckEntry(filter);
240
241                         if( child != null)                      {
242                                 if(child.Properties["objectclass"].ContainsCaselessStringValue(otype))
243                                         return child;
244                                 else
245                                         throw new Exception("An unknown directory object was requested");
246                         }
247                         return child;
248                 }
249
250                 /// <summary>
251                 /// Checks whether the entry with the specified Relative distinguised name
252                 /// exists or not.
253                 /// </summary>
254                 /// <param name="rdn"> Relative distinguished name of the entry</param>
255                 /// <returns>DirectoryEntry object of Entry if entry exists,
256                 /// Null if entry doesn't exist </returns>
257                 private DirectoryEntry CheckEntry(string rdn)
258                 {
259                         string Ofdn=null;
260                         DirectoryEntry cEntry=null;
261
262                         Ofdn=rdn+","+Basedn;
263                         string[] attrs={"objectClass"};
264                         try                                                                             {
265                                 LdapSearchResults lsc= Conn.Search(     Ofdn,
266                                                                                                         LdapConnection.SCOPE_BASE,
267                                                                                                         "objectClass=*",
268                                                                                                         attrs,
269                                                                                                         false);
270                                 while(lsc.hasMore())                            {
271                                         LdapEntry nextEntry = null;
272                                         try                                                             {
273                                                 nextEntry = lsc.next();
274                                                 cEntry =  new DirectoryEntry(Conn);
275                                                 LdapUrl Burl=new LdapUrl(_Bpath);
276                                                 LdapUrl curl=new LdapUrl(Burl.Host,Burl.Port,Ofdn);
277                                                 cEntry.Path=curl.ToString();
278                                         }
279                                         catch(LdapException e)                  {
280                                                 Console.WriteLine("Error: " + e.LdapErrorMessage);
281                                                 // Exception is thrown, go for next entry
282                                                 throw e;
283                                         }
284                                         break;
285                                 }
286
287                         }
288                         catch(LdapException le)
289                         {
290                                 if(le.ResultCode == LdapException.NO_SUCH_OBJECT)       {
291                                         return null;
292                                 }
293                                 else            {
294                                         throw le;
295                                 }
296                         }
297                         catch(Exception e)              {
298                                 throw e;
299                         }
300                         return cEntry;
301                 }
302
303         }
304
305 }
306