Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- using SecurityInterface;
- using GlobalFunctions;
- namespace LdapSecurityPlugin
- {
- public partial class LdapSecurity : ISecurityPlugin
- {
- private DataSet dataSetPointer;
- private Int64 securityProfileId;
- private static LoadingDialog loading;
- private static String message;
- private StringResources.Translate Strings;
- public bool saveCredentials = false;
- DataRow saveCredRow = null;
- public LdapSecurity()
- {
- }
- public String DisplayName
- {
- get
- {
- return SecurityTypes.LDAP.ToString();
- }
- }
- public Int64 SecurityProfileId
- {
- get
- {
- return securityProfileId;
- }
- set
- {
- securityProfileId = value;
- }
- }
- public Boolean AllowsTesting
- {
- get
- {
- return true;
- }
- }
- public SecurityTypes UniqueId
- {
- get
- {
- return SecurityTypes.LDAP;
- }
- }
- //this method is not used. It is only here because it must be defined to comply with ISecurityPlugin. (NTML Security uses it)
- public Boolean SaveCredentials
- {
- get
- {
- return saveCredentials;
- }
- set
- {
- saveCredentials = value;
- }
- }
- public String checkForMissingValues(Object currentPropertiesGrid)
- {
- ArrayList values = new ArrayList();
- PropertiesClass props = (PropertiesClass)currentPropertiesGrid;
- if (props.Server == null || props.Server.Trim().Equals(""))
- {
- values.Add(Strings.ReturnString("SERVER"));
- }
- if (props.Domain == null || props.Domain.Trim().Equals(""))
- {
- values.Add(Strings.ReturnString("DOMAIN"));
- }
- if (props.UserId == null || props.UserId.Trim().Equals(""))
- {
- values.Add(Strings.ReturnString("GUEST_ID"));
- }
- if (props.Scope == null || props.Scope.Trim().Equals(""))
- {
- values.Add(Strings.ReturnString("SEARCH_SCOPE"));
- }
- if (values.Count == 0)
- {
- return null;
- }
- else
- {
- String retVal = Strings.ReturnString("FOLLOWING_PARAMETERS_MISSING_VALUES") + "\n\n";
- for (int a = 0; a < values.Count; a++)
- {
- retVal += values[a].ToString() + "\n";
- }
- return retVal;
- }
- }
- public void Initialize(DataSet dataStoragePointer, StringResources.Translate StringsInput)
- {
- this.dataSetPointer = dataStoragePointer;
- this.Strings = StringsInput;
- }
- public Object returnPropertiesClass()
- {
- return new PropertiesClass();
- }
- public Object returnPropertiesClass(DataSet dataSet, Int64 securityProfileIndex)
- {
- PropertiesClass props = new PropertiesClass();
- DataRow[] rows = dataSet.Tables["Security Profile Params"].Select("[Security Profile Id] = " + securityProfileIndex);
- for (int a = 0; a < rows.Length; a++)
- {
- if (rows[a].RowState != DataRowState.Deleted)
- {
- if (rows[a]["Name"].Equals("Server"))
- {
- props.Server = rows[a]["Value"].ToString();
- }
- else if (rows[a]["Name"].Equals("Domain"))
- {
- props.Domain = rows[a]["Value"].ToString();
- }
- else if (rows[a]["Name"].Equals("Guest Id"))
- {
- props.UserId = rows[a]["Value"].ToString();
- }
- else if (rows[a]["Name"].Equals("Guest Password"))
- {
- props.Password = rows[a]["Value"].ToString();
- }
- else if (rows[a]["Name"].Equals("Scope"))
- {
- props.Scope = rows[a]["Value"].ToString();
- }
- }
- }
- return props;
- }
- public void saveParameters(DataSet dataSet, Int64 securityProfileIndex, Object parameters)
- {
- PropertiesClass props = (PropertiesClass)parameters;
- DataRow[] rows = dataSet.Tables["Security Profile Params"].Select("[Security Profile Id] = " + securityProfileIndex);
- for (int a = 0; a < rows.Length; a++)
- {
- if (rows[a].RowState != DataRowState.Deleted)
- {
- dataSet.Tables["Security Profile Params"].Rows[dataSet.Tables["Security Profile Params"].Rows.IndexOf(rows[a])].Delete();
- }
- }
- DataRow row = dataSet.Tables["Security Profile Params"].NewRow();
- row["Name"] = "Server";
- if (!String.IsNullOrEmpty(props.Server))
- row["Value"] = props.Server;
- else
- row["Value"] = "";
- row["Security Profile Id"] = securityProfileIndex;
- dataSet.Tables["Security Profile Params"].Rows.Add(row);
- row = dataSet.Tables["Security Profile Params"].NewRow();
- row["Name"] = "Domain";
- if (!String.IsNullOrEmpty(props.Domain))
- row["Value"] = props.Domain;
- else
- row["Value"] = "";
- row["Security Profile Id"] = securityProfileIndex;
- dataSet.Tables["Security Profile Params"].Rows.Add(row);
- row = dataSet.Tables["Security Profile Params"].NewRow();
- row["Name"] = "Guest Id";
- if (!String.IsNullOrEmpty(props.UserId))
- row["Value"] = props.UserId;
- else
- row["Value"] = "";
- row["Security Profile Id"] = securityProfileIndex;
- dataSet.Tables["Security Profile Params"].Rows.Add(row);
- row = dataSet.Tables["Security Profile Params"].NewRow();
- row["Name"] = "Guest Password";
- if (!String.IsNullOrEmpty(props.Password))
- row["Value"] = props.Password;
- else
- row["Value"] = "";
- row["Security Profile Id"] = securityProfileIndex;
- dataSet.Tables["Security Profile Params"].Rows.Add(row);
- row = dataSet.Tables["Security Profile Params"].NewRow();
- row["Name"] = "Scope";
- row["Value"] = props.Scope.ToString();
- row["Security Profile Id"] = securityProfileIndex;
- dataSet.Tables["Security Profile Params"].Rows.Add(row);
- }
- public LogonReturnValues performLogonCheck(String userName, String password, SecurityParam[] logonParams,
- out Dictionary<String,String> storeParams)
- {
- String server = "";
- String domain = "";
- String guestId = "";
- String guestPassword = "";
- String scope = "";
- storeParams = new Dictionary<String,String>();
- for (int a = 0; a < logonParams.Length; a++)
- {
- if (String.Compare(logonParams[a].Name, "Domain") == 0)
- {
- domain = logonParams[a].Value;
- }
- else if (String.Compare(logonParams[a].Name, "Guest Id") == 0)
- {
- guestId = logonParams[a].Value;
- }
- else if (String.Compare(logonParams[a].Name, "Guest Password") == 0)
- {
- guestPassword = logonParams[a].Value;
- }
- else if (String.Compare(logonParams[a].Name, "Server") == 0)
- {
- server = logonParams[a].Value;
- }
- else if (String.Compare(logonParams[a].Name, "Scope") == 0)
- {
- scope = logonParams[a].Value;
- }
- }
- LogonReturnValues returnVal = new LogonReturnValues();
- if (String.IsNullOrEmpty(server) || String.IsNullOrEmpty(domain) ||
- String.IsNullOrEmpty(guestId) || String.IsNullOrEmpty(guestPassword))
- {
- //Missing parameters
- returnVal.Response = (int)LogonErrors.MISSING_VALUES;
- return returnVal;
- }
- //Logon successful
- Ldap ldap = new Ldap(guestId, guestPassword, server, domain, scope);
- //perform test
- returnVal.Response = (int)ldap.checkPassword(userName, password);
- if (returnVal.Response == (int)LogonErrors.NONE)
- {
- returnVal.EmailAddress = ldap.returnEmailAddress();
- returnVal.FullName = ldap.returnFullName();
- returnVal.ResolvedUserName = ldap.returnResolvedAccountName();
- //Determine what the domain is
- returnVal.Domain = generateDomain(domain);
- storeParams.Add("HomeDirectory", ldap.returnHomeDirectory());
- storeParams.Add("DisplayName", ldap.returnDisplayName());
- storeParams.Add("AccountName", ldap.returnAccountName());
- storeParams.Add("DistinguishedName", ldap.returnDistinguishedName());
- }
- return returnVal;
- }
- private String generateDomain(String ldapValue)
- {
- String generatedDomain = "";
- try
- {
- String[] vals = ldapValue.Split(new char[] { ',' });
- for (int a = 0; a < vals.Length; a++)
- {
- if (!(vals[a] == null) && !(vals[a].Trim().Equals("")) &&
- vals[a].Substring(0, 3).Equals("DC=", StringComparison.CurrentCultureIgnoreCase))
- {
- if (vals[a].Length > 3)
- {
- generatedDomain += vals[a].Substring(3);
- if (a != vals.Length - 1)
- generatedDomain += ".";
- }
- }
- }
- }
- catch (Exception)
- {
- generatedDomain = null;
- }
- return generatedDomain;
- }
- public String performSetupTest(Object parameters)
- {
- PropertiesClass props = (PropertiesClass) parameters;
- if (String.IsNullOrEmpty(props.Server) || String.IsNullOrEmpty(props.Server.Trim()))
- {
- return String.Format(Strings.ReturnString("VALUE_NOT_SET"), Strings.ReturnString("SERVER"));
- }
- if (String.IsNullOrEmpty(props.Domain) || String.IsNullOrEmpty(props.Domain.Trim()))
- {
- return String.Format(Strings.ReturnString("VALUE_NOT_SET"), Strings.ReturnString("DOMAIN"));
- }
- if (String.IsNullOrEmpty(props.UserId) || String.IsNullOrEmpty(props.UserId.Trim()))
- {
- return String.Format(Strings.ReturnString("VALUE_NOT_SET"), Strings.ReturnString("GUEST_ID"));
- }
- if (String.IsNullOrEmpty(props.Password) || String.IsNullOrEmpty(props.Password.Trim()))
- {
- return String.Format(Strings.ReturnString("VALUE_NOT_SET"), Strings.ReturnString("GUEST_PASSWORD"));
- }
- if (String.IsNullOrEmpty(props.Scope) || String.IsNullOrEmpty(props.Scope.Trim()))
- {
- return String.Format(Strings.ReturnString("VALUE_NOT_SET"), Strings.ReturnString("SEARCH_SCOPE"));
- }
- //#region test connection - bypasses test and returns sucess
- loading = new LoadingDialog(Strings.ReturnString("PLEASE_WAIT"), String.Format(Strings.ReturnString("CHECKING_VALUE_SETTINGS"), "LDAP"), Strings.ReturnString("CANCEL"));
- LdapThread ldap = new LdapThread(props);
- Thread thr = new Thread(new ThreadStart(ldap.ThreadTask));
- thr.Start();
- if (loading.ShowDialog() == DialogResult.Abort)
- {
- loading.Close();
- thr.Abort();
- return "-1";
- }
- else
- {
- if (message.Equals(""))
- {
- return null;
- }
- else
- {
- return message;
- }
- }
- //#endregion
- //return null;
- }
- public void saveTestCredentials(DataSet dataSet, long index)
- {
- dataSet.Tables["Security Profiles Stored Logins"].Rows.Add(saveCredRow);
- }
- class LdapThread
- {
- private PropertiesClass props = null;
- public LdapThread(PropertiesClass props)
- {
- this.props = props;
- }
- public void ThreadTask()
- {
- Thread.Sleep(500);
- Ldap ldap = new Ldap(props.UserId, props.Password, props.Server, props.Domain, props.Scope);
- message = ldap.checkServer();
- if (loading.InvokeRequired)
- loading.Invoke(new MethodInvoker(loading.Close));
- else
- loading.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment