/****************************************************************************** * The MIT License * Copyright (c) 2003 Novell Inc., www.novell.com * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the Software), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. *******************************************************************************/ // // System.DirectoryServices.DirectoryEntries.cs // // Author: // Sunil Kumar (sunilk@novell.com) // // (C) Novell Inc. // using System.Collections; using Novell.Directory.Ldap; namespace System.DirectoryServices { /// ///Contains the children (child entries) of an entry in /// a Ldap Directory /// public class DirectoryEntries : IEnumerable { private ArrayList cList; private LdapConnection _Conn=null; private string _Bpath=null; private string _Buser=null; private string _Bpass=null; private string _Basedn=null; protected ArrayList m_oValues=null; /// Initializes the Connection and other properties. /// /// private void InitBlock() { try { LdapUrl lUrl=new LdapUrl(_Bpath); _Conn = new LdapConnection(); _Conn.Connect(lUrl.Host,lUrl.Port); _Conn.Bind(_Buser,_Bpass); } catch(LdapException ex) { Console.WriteLine("Error:" + ex.LdapErrorMessage); throw ex; } catch(Exception e) { Console.WriteLine("Error:" + e.Message); throw e; } } internal string Basedn { get { if( _Basedn == null) { // Console.WriteLine("Basepath:" + _Bpath); LdapUrl lurl=new LdapUrl(_Bpath); string bdn = lurl.getDN(); if( bdn != null) _Basedn = bdn; else _Basedn = ""; } return _Basedn; } } /// Contains the Path of the Container under which /// the entries belongs to. /// internal string Bpath { get { return _Bpath; } set { _Bpath=value; } } /// Returns the connection object used to communicate with /// Ldap server /// internal LdapConnection Conn { get { if( _Conn == null) { InitBlock(); } return _Conn; } set { _Conn=value; } } /// Constructs a collection of all the child entries of /// an entry /// /// Path of the entry /// /// Username to Bind as while authenticating to ldap /// server /// Password of the user internal DirectoryEntries(string path, string uname, string passwd) { _Bpath = path; _Buser = uname; _Bpass = passwd; } /// Constructs a collection of all the child entries of /// a entry /// /// Path of the entry /// /// connection object used to connect to ldap server /// internal DirectoryEntries(string path, LdapConnection lc) { _Bpath = path; _Conn = lc; } public IEnumerator GetEnumerator() { m_oValues= new ArrayList(); string[] attrs={"objectClass"}; // Console.WriteLine("BaseDN is:" + Basedn); LdapSearchResults lsc= Conn.Search( Basedn, LdapConnection.SCOPE_ONE, "objectClass=*", attrs, false); LdapUrl Burl=new LdapUrl(_Bpath); string host=Burl.Host; int port=Burl.Port; while (lsc.hasMore()) { LdapEntry nextEntry = null; try { nextEntry = lsc.next(); } catch(LdapException e) { Console.WriteLine("Error: " + e.LdapErrorMessage); // Exception is thrown, go for next entry continue; } DirectoryEntry dEntry=new DirectoryEntry(Conn); string eFdn=nextEntry.DN; LdapUrl curl=new LdapUrl(host,port,eFdn); dEntry.Path=curl.ToString(); m_oValues.Add((DirectoryEntry) dEntry); } return m_oValues.GetEnumerator(); } /// Creates a request to create a new entry in the container. /// /// /// RDN of the entry to be created /// /// StructuralClassName of the entry to be /// created. /// public DirectoryEntry Add( string name,string schemaClassName) { DirectoryEntry ent=new DirectoryEntry(Conn); LdapUrl Burl=new LdapUrl(_Bpath); string eFdn=name+","+Burl.getDN(); LdapUrl curl=new LdapUrl(Burl.Host,Burl.Port,eFdn); ent.Path=curl.ToString(); ent.Nflag = true; ent.Properties["objectclass"].Add(schemaClassName); return ent; } /// /// Deletes a child DirectoryEntry from this collection /// /// The DirectoryEntry to delete public void Remove( DirectoryEntry entry ) { LdapUrl Burl=new LdapUrl(_Bpath); string eFDN = entry.Name + "," + Burl.getDN(); Conn.Delete( eFDN); } /// /// Returns the child with the specified name. /// /// relative distinguised name of the child /// /// Child entry with the specified name public DirectoryEntry Find(string filter) { DirectoryEntry child=CheckEntry(filter); return child; } /// /// Returns the child with the specified name and of the specified type. /// /// relative distinguised name of the child /// /// Type of the child i.e strutcuralObjectClass /// name of the child /// Child entry with the specified name and type public DirectoryEntry Find(string filter, string otype) { DirectoryEntry child=CheckEntry(filter); if( child != null) { if(child.Properties["objectclass"].ContainsCaselessStringValue(otype)) return child; else throw new Exception("An unknown directory object was requested"); } return child; } /// /// Checks whether the entry with the specified Relative distinguised name /// exists or not. /// /// Relative distinguished name of the entry /// DirectoryEntry object of Entry if entry exists, /// Null if entry doesn't exist private DirectoryEntry CheckEntry(string rdn) { string Ofdn=null; DirectoryEntry cEntry=null; Ofdn=rdn+","+Basedn; string[] attrs={"objectClass"}; try { LdapSearchResults lsc= Conn.Search( Ofdn, LdapConnection.SCOPE_BASE, "objectClass=*", attrs, false); while(lsc.hasMore()) { LdapEntry nextEntry = null; try { nextEntry = lsc.next(); cEntry = new DirectoryEntry(Conn); LdapUrl Burl=new LdapUrl(_Bpath); LdapUrl curl=new LdapUrl(Burl.Host,Burl.Port,Ofdn); cEntry.Path=curl.ToString(); } catch(LdapException e) { Console.WriteLine("Error: " + e.LdapErrorMessage); // Exception is thrown, go for next entry throw e; } break; } } catch(LdapException le) { if(le.ResultCode == LdapException.NO_SUCH_OBJECT) { return null; } else { throw le; } } catch(Exception e) { throw e; } return cEntry; } } }