1 /******************************************************************************
3 * Copyright (c) 2003 Novell Inc., www.novell.com
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
22 *******************************************************************************/
25 // System.DirectoryServices.DirectoryEntries.cs
28 // Sunil Kumar (sunilk@novell.com)
33 using System
.Collections
;
34 using Novell
.Directory
.Ldap
;
36 namespace System
.DirectoryServices
40 ///Contains the children (child entries) of an entry in
43 public class DirectoryEntries
: IEnumerable
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;
53 /// <summary> Initializes the Connection and other properties.
56 private void InitBlock()
59 LdapUrl lUrl
=new LdapUrl(_Bpath
);
60 _Conn
= new LdapConnection();
61 _Conn
.Connect(lUrl
.Host
,lUrl
.Port
);
62 _Conn
.Bind(_Buser
,_Bpass
);
64 catch(LdapException ex
) {
72 internal string Basedn
75 if( _Basedn
== null) {
76 LdapUrl lurl
=new LdapUrl(_Bpath
);
77 string bdn
= lurl
.getDN();
87 /// <summary> Contains the Path of the Container under which
88 /// the entries belongs to.
100 /// <summary> Returns the connection object used to communicate with
103 internal LdapConnection Conn
116 /// <summary> Constructs a collection of all the child entries of
119 /// <param name="path"> Path of the entry
121 /// <param name="uname"> Username to Bind as while authenticating to ldap
123 /// <param name="passwd"> Password of the user</param>
124 internal DirectoryEntries(string path
, string uname
, string passwd
)
131 /// <summary> Constructs a collection of all the child entries of
134 /// <param name="path"> Path of the entry
136 /// <param name="lc"> connection object used to connect to ldap server
138 internal DirectoryEntries(string path
, LdapConnection lc
)
144 public SchemaNameCollection SchemaFilter
{
146 get { throw new NotImplementedException ("System.DirectoryServices.DirectoryEntries.SchemaFilter"); }
149 public IEnumerator
GetEnumerator()
151 m_oValues
= new ArrayList();
152 string[] attrs
={"objectClass"}
;
153 LdapSearchResults lsc
= Conn
.Search( Basedn
,
154 LdapConnection
.SCOPE_ONE
,
159 LdapUrl Burl
=new LdapUrl(_Bpath
);
160 string host
=Burl
.Host
;
163 while (lsc
.hasMore()) {
164 LdapEntry nextEntry
= null;
166 nextEntry
= lsc
.next();
168 catch(LdapException e
) {
169 // Exception is thrown, go for next entry
172 DirectoryEntry dEntry
=new DirectoryEntry(Conn
);
173 string eFdn
=nextEntry
.DN
;
174 LdapUrl curl
=new LdapUrl(host
,port
,eFdn
);
175 dEntry
.Path
=curl
.ToString();
176 m_oValues
.Add((DirectoryEntry
) dEntry
);
178 return m_oValues
.GetEnumerator();
181 /// <summary> Creates a request to create a new entry in the container.
184 /// <param name="name"> RDN of the entry to be created
186 /// <param name="schemaClassName"> StructuralClassName of the entry to be
189 public DirectoryEntry
Add( string name
,string schemaClassName
)
191 DirectoryEntry ent
=new DirectoryEntry(Conn
);
192 LdapUrl Burl
=new LdapUrl(_Bpath
);
193 string baseDn
= Burl
.getDN();
194 string eFdn
=((baseDn
!= null && baseDn
.Length
!= 0) ? (name
+ "," + baseDn
) : name
);
195 LdapUrl curl
=new LdapUrl(Burl
.Host
,Burl
.Port
,eFdn
);
196 ent
.Path
=curl
.ToString();
202 /// Deletes a child DirectoryEntry from this collection
204 /// <param name="entry">The DirectoryEntry to delete</param>
205 public void Remove( DirectoryEntry entry
)
207 LdapUrl Burl
=new LdapUrl(_Bpath
);
208 string eFDN
= entry
.Name
+ "," + Burl
.getDN();
213 /// Returns the child with the specified name.
215 /// <param name="filter">relative distinguised name of the child
217 /// <returns>Child entry with the specified name </returns>
218 public DirectoryEntry
Find(string filter
)
220 DirectoryEntry child
=CheckEntry(filter
);
225 /// Returns the child with the specified name and of the specified type.
227 /// <param name="filter">relative distinguised name of the child
229 /// <param name="otype"> Type of the child i.e strutcuralObjectClass
230 /// name of the child </param>
231 /// <returns>Child entry with the specified name and type</returns>
232 public DirectoryEntry
Find(string filter
, string otype
)
234 DirectoryEntry child
=CheckEntry(filter
);
237 if(child
.Properties
["objectclass"].ContainsCaselessStringValue(otype
))
240 throw new SystemException("An unknown directory object was requested");
246 /// Checks whether the entry with the specified Relative distinguised name
249 /// <param name="rdn"> Relative distinguished name of the entry</param>
250 /// <returns>DirectoryEntry object of Entry if entry exists,
251 /// Null if entry doesn't exist </returns>
252 private DirectoryEntry
CheckEntry(string rdn
)
255 DirectoryEntry cEntry
=null;
258 string[] attrs
={"objectClass"}
;
260 LdapSearchResults lsc
= Conn
.Search( Ofdn
,
261 LdapConnection
.SCOPE_BASE
,
265 while(lsc
.hasMore()) {
266 LdapEntry nextEntry
= null;
268 nextEntry
= lsc
.next();
269 cEntry
= new DirectoryEntry(Conn
);
270 LdapUrl Burl
=new LdapUrl(_Bpath
);
271 LdapUrl curl
=new LdapUrl(Burl
.Host
,Burl
.Port
,Ofdn
);
272 cEntry
.Path
=curl
.ToString();
274 catch(LdapException e
) {
275 // Exception is thrown, go for next entry
282 catch(LdapException le
)
284 if(le
.ResultCode
== LdapException
.NO_SUCH_OBJECT
) {