context: /*****************************************************************************
  *
  * Copyright (c) [2009] Novell, Inc.
  * All Rights Reserved.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public License as
  * published by the Free Software Foundation.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, contact Novell, Inc.
  *
  * To contact Novell about this file by physical or electronic mail,
  * you may find current contact information at www.novell.com
  *
  *-----------------------------------------------------------------------------
  *
  *                 $Author: Brady Anderson <banderso@novell.com>
  *                 $Modified by: <Modifier>
  *                 $Mod Date: <Date Modified>
  *                 $Revision: 0.1
  *-----------------------------------------------------------------------------
  * This module is used to:
  *        <Description of the functionality of the file >
  *
  *
  *******************************************************************************/
  
  using System;
  using System.Reflection;
  using System.Collections;
  using System.Net;
  using System.Text;
  using System.Web;
  using System.Xml;
  using System.Security.Cryptography;
  using System.Security.Cryptography.X509Certificates;
  
  using Syscert = System.Security.Cryptography.X509Certificates;
  using Mono.Security.X509;
  using Mono.Security.Cryptography;
  
  using Novell.Directory.Ldap;
  using Novell.Directory.Ldap.Utilclass;
  
  using Simias;
  using Simias.Storage;
  using Simias.Sync;
  using Simias.Server;
  
  using SCodes = Simias.Authentication.StatusCodes;
  
  namespace Simias.LdapProvider
  {
  	/// <summary>
  	/// Implementation of the IUserProvider Service for
  	/// the ldap provider.
  	///
  	/// NOTE! This ldap provider implementation does NOT allow 
  	/// user creation and deletion via the IUserProvider interface
  	///	so those methods will return false.
  	/// </summary>
  	public class User : Simias.Server.IUserProvider
  	{
  		#region Fields
  		/// <summary>
  		/// Used to log messages.
  		/// </summary>
  		private static readonly ISimiasLog log = 
  			SimiasLogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType );
  
  		/// <summary>
  		/// String used to identify domain provider.
  		/// </summary>
  		static private string providerName = "LDAP Provider";
  		static private string providerDescription = "A provider to sync identities from a LDAP to a Simias domain";
  		
  		static private string missingDomainMessage = "Enterprise domain does not exist!";
  
  		// Frequently used Simias types
  		private Store store = null;
  		private Simias.Storage.Domain domain = null;
  		private LdapSettings ldapSettings = null;
  		#endregion
  		
  		#region Properties
  		/// <summary>
  		/// Gets the name of the provider.
  		/// </summary>
  		public string Name { get { return providerName; } }
  
  		/// <summary>
  		/// Gets the description of the provider.
  		/// </summary>
  		public string Description { get { return providerDescription; } }
  		#endregion
  
  		private bool CertFailure = false;
  		
                  public enum PasswordChangeStatus
                  {
                          /// <summary>
                          /// Password Change is successful
                          /// </summary>
                          Success=0,
  
                          /// <summary>
                          /// Invalid Old Passowrd provided
                          /// </summary>
                          IncorrectOldPassword,
  
                          /// <summary>
                          /// Failed to reset password
                          /// </summary>
  			FailedToResetPassword, 
  
                          /// <summary>
                          /// Login Disabled
                          /// </summary>
  			LoginDisabled, 
  
                          /// <summary>
                          /// User account expired
                          /// </summary>
  			UserAccountExpired, 
  
                          /// <summary>
                          /// User can not change password
                          /// </summary>
  			CanNotChangePassword, 
  
                          /// <summary>
                          /// User password expired
                          /// </summary>
  			LoginPasswordExpired, 
  
                          /// <summary>
                          /// Minimum password length restriction not met
                          /// </summary>
  			PasswordMinimumLength,
  
                          /// <summary>
                          /// User not found in simias
                          /// </summary>
  			UserNotFoundInSimias
                  };
  
  
  		#region Constructor
  		/// <summary>
  		/// Initializes an instance of this object.
  		/// </summary>
  		public User()
  		{
  			store = Store.GetStore();
  			if ( store.DefaultDomain == null )
  			{
  				throw new SimiasException( User.missingDomainMessage );
  			}
  			
  			// Is the default domain always the correct domain?
  			domain = store.GetDomain( store.DefaultDomain );
  			if ( domain == null )
  			{
  				throw new SimiasException( User.missingDomainMessage );
  			}
  			
  			if ( domain.IsType( "Enterprise" ) == false )
  			{
  				throw new SimiasException( User.missingDomainMessage );
  			}
  			
  			ldapSettings = LdapSettings.Get( Store.StorePath );
  			
  			
  			// Make sure the password is set on the admin
  			SetAdminPassword();
  		}
  		#endregion
  		
  		#region Private Methods
  
          /// <summary>
          /// When the Enterprise Server Domain is instantiated in Simias.Server
          /// the domain owner is determined via the AdminName in Simias.config.
          /// Simple Server determines the domain owner via an attribute in
          /// the SimpleServer.xml file so this method will actually change
          /// ownership of the domain to the one specified in the xml file
          /// unless they happen to be the same.
          /// </summary>
          /// <returns></returns>
  		private bool SetAdminPassword()
  		{
  			bool status = false;
  
  			/*
  			// Domain administrator is determined by "owner" attribute
  			// in the SimpleServer.xml document
  			
  			string identityDocumentPath = "../../etc/SimpleServer.xml";
  			string owner = null;
  			string ownerPassword = null;
  
  			try
  			{
  				// Load the SimpleServer domain and memberlist XML file.
  				XmlDocument serverDoc = new XmlDocument();
  				serverDoc.Load( identityDocumentPath );
  				XmlElement domainElement = serverDoc.DocumentElement;
  				XmlAttribute attr;
  				
  				for ( int i = 0; i < domainElement.ChildNodes.Count; i++ )
  				{
  					attr = domainElement.ChildNodes[i].Attributes[ "Owner" ];
  					if (attr != null)
  					{
  						XmlNode cNode = domainElement.ChildNodes[i];
  						owner = cNode.Attributes[ "Name" ].Value;
  						ownerPassword = cNode.Attributes[ "Password" ].Value;
  						break;
  					}	
  				}
  			}
  			catch(Exception e)
  			{
  				log.Error( e.Message );
  
gt: 				log.Error( e.StackTrace );