Guest User

LDAPNew

a guest
Feb 7th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.07 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10.  
  11. using SecurityInterface;
  12. using GlobalFunctions;
  13.  
  14. namespace LdapSecurityPlugin
  15. {
  16.     public partial class LdapSecurity : ISecurityPlugin
  17.     {
  18.         private DataSet dataSetPointer;
  19.         private Int64 securityProfileId;
  20.         private static LoadingDialog loading;
  21.         private static String message;
  22.         private StringResources.Translate Strings;
  23.  
  24.         public bool saveCredentials = false;
  25.         DataRow saveCredRow = null;
  26.  
  27.         public LdapSecurity()
  28.         {
  29.         }
  30.  
  31.         public String DisplayName
  32.         {
  33.             get
  34.             {
  35.                 return SecurityTypes.LDAP.ToString();
  36.             }
  37.         }
  38.  
  39.         public Int64 SecurityProfileId
  40.         {
  41.             get
  42.             {
  43.                 return securityProfileId;
  44.             }
  45.  
  46.             set
  47.             {
  48.                 securityProfileId = value;
  49.             }
  50.         }
  51.  
  52.         public Boolean AllowsTesting
  53.         {
  54.             get
  55.             {
  56.                 return true;
  57.             }
  58.         }
  59.  
  60.         public SecurityTypes UniqueId
  61.         {
  62.             get
  63.             {
  64.                 return SecurityTypes.LDAP;
  65.             }
  66.         }
  67.  
  68.         //this method is not used. It is only here because it must be defined to comply with ISecurityPlugin. (NTML Security uses it)
  69.         public Boolean SaveCredentials
  70.         {
  71.             get
  72.             {
  73.                 return saveCredentials;
  74.             }
  75.             set
  76.             {
  77.                 saveCredentials = value;
  78.             }
  79.         }
  80.  
  81.         public String checkForMissingValues(Object currentPropertiesGrid)
  82.         {
  83.             ArrayList values = new ArrayList();
  84.             PropertiesClass props = (PropertiesClass)currentPropertiesGrid;
  85.  
  86.             if (props.Server == null || props.Server.Trim().Equals(""))
  87.             {
  88.                 values.Add(Strings.ReturnString("SERVER"));
  89.             }
  90.             if (props.Domain == null || props.Domain.Trim().Equals(""))
  91.             {
  92.                 values.Add(Strings.ReturnString("DOMAIN"));
  93.             }
  94.             if (props.UserId == null || props.UserId.Trim().Equals(""))
  95.             {
  96.                 values.Add(Strings.ReturnString("GUEST_ID"));
  97.             }
  98.             if (props.Scope == null || props.Scope.Trim().Equals(""))
  99.             {
  100.                 values.Add(Strings.ReturnString("SEARCH_SCOPE"));
  101.             }
  102.  
  103.             if (values.Count == 0)
  104.             {
  105.                 return null;
  106.             }
  107.             else
  108.             {
  109.                 String retVal = Strings.ReturnString("FOLLOWING_PARAMETERS_MISSING_VALUES") + "\n\n";
  110.  
  111.                 for (int a = 0; a < values.Count; a++)
  112.                 {
  113.                     retVal += values[a].ToString() + "\n";
  114.                 }
  115.                 return retVal;
  116.             }
  117.         }
  118.  
  119.         public void Initialize(DataSet dataStoragePointer, StringResources.Translate StringsInput)
  120.         {
  121.             this.dataSetPointer = dataStoragePointer;
  122.             this.Strings = StringsInput;
  123.         }
  124.  
  125.         public Object returnPropertiesClass()
  126.         {
  127.             return new PropertiesClass();
  128.         }
  129.  
  130.         public Object returnPropertiesClass(DataSet dataSet, Int64 securityProfileIndex)
  131.         {
  132.             PropertiesClass props = new PropertiesClass();
  133.  
  134.             DataRow[] rows = dataSet.Tables["Security Profile Params"].Select("[Security Profile Id] = " + securityProfileIndex);
  135.  
  136.             for (int a = 0; a < rows.Length; a++)
  137.             {
  138.                 if (rows[a].RowState != DataRowState.Deleted)
  139.                 {
  140.                     if (rows[a]["Name"].Equals("Server"))
  141.                     {
  142.                         props.Server = rows[a]["Value"].ToString();
  143.                     }
  144.                     else if (rows[a]["Name"].Equals("Domain"))
  145.                     {
  146.                         props.Domain = rows[a]["Value"].ToString();
  147.                     }
  148.                     else if (rows[a]["Name"].Equals("Guest Id"))
  149.                     {
  150.                         props.UserId = rows[a]["Value"].ToString();
  151.                     }
  152.                     else if (rows[a]["Name"].Equals("Guest Password"))
  153.                     {
  154.                         props.Password = rows[a]["Value"].ToString();
  155.                     }
  156.                     else if (rows[a]["Name"].Equals("Scope"))
  157.                     {
  158.                         props.Scope = rows[a]["Value"].ToString();
  159.                     }
  160.                 }
  161.             }
  162.  
  163.             return props;
  164.         }
  165.  
  166.         public void saveParameters(DataSet dataSet, Int64 securityProfileIndex, Object parameters)
  167.         {
  168.             PropertiesClass props = (PropertiesClass)parameters;
  169.  
  170.             DataRow[] rows = dataSet.Tables["Security Profile Params"].Select("[Security Profile Id] = " + securityProfileIndex);
  171.  
  172.             for (int a = 0; a < rows.Length; a++)
  173.             {
  174.                 if (rows[a].RowState != DataRowState.Deleted)
  175.                 {
  176.                     dataSet.Tables["Security Profile Params"].Rows[dataSet.Tables["Security Profile Params"].Rows.IndexOf(rows[a])].Delete();
  177.                 }
  178.             }
  179.  
  180.             DataRow row = dataSet.Tables["Security Profile Params"].NewRow();
  181.             row["Name"] = "Server";
  182.             if (!String.IsNullOrEmpty(props.Server))
  183.                 row["Value"] = props.Server;
  184.             else
  185.                 row["Value"] = "";
  186.             row["Security Profile Id"] = securityProfileIndex;
  187.             dataSet.Tables["Security Profile Params"].Rows.Add(row);
  188.  
  189.             row = dataSet.Tables["Security Profile Params"].NewRow();
  190.             row["Name"] = "Domain";
  191.             if (!String.IsNullOrEmpty(props.Domain))
  192.                 row["Value"] = props.Domain;
  193.             else
  194.                 row["Value"] = "";
  195.             row["Security Profile Id"] = securityProfileIndex;
  196.             dataSet.Tables["Security Profile Params"].Rows.Add(row);
  197.  
  198.             row = dataSet.Tables["Security Profile Params"].NewRow();
  199.             row["Name"] = "Guest Id";
  200.             if (!String.IsNullOrEmpty(props.UserId))
  201.                 row["Value"] = props.UserId;
  202.             else
  203.                 row["Value"] = "";
  204.             row["Security Profile Id"] = securityProfileIndex;
  205.             dataSet.Tables["Security Profile Params"].Rows.Add(row);
  206.  
  207.             row = dataSet.Tables["Security Profile Params"].NewRow();
  208.             row["Name"] = "Guest Password";
  209.             if (!String.IsNullOrEmpty(props.Password))
  210.                 row["Value"] = props.Password;
  211.             else
  212.                 row["Value"] = "";
  213.             row["Security Profile Id"] = securityProfileIndex;
  214.             dataSet.Tables["Security Profile Params"].Rows.Add(row);
  215.  
  216.             row = dataSet.Tables["Security Profile Params"].NewRow();
  217.             row["Name"] = "Scope";
  218.             row["Value"] = props.Scope.ToString();
  219.             row["Security Profile Id"] = securityProfileIndex;
  220.             dataSet.Tables["Security Profile Params"].Rows.Add(row);
  221.         }
  222.  
  223.         public LogonReturnValues performLogonCheck(String userName, String password, SecurityParam[] logonParams,
  224.                                                    out Dictionary<String,String> storeParams)
  225.         {
  226.             String server = "";
  227.             String domain = "";
  228.             String guestId = "";
  229.             String guestPassword = "";
  230.             String scope = "";
  231.  
  232.             storeParams = new Dictionary<String,String>();
  233.  
  234.             for (int a = 0; a < logonParams.Length; a++)
  235.             {
  236.                 if (String.Compare(logonParams[a].Name, "Domain") == 0)
  237.                 {
  238.                     domain = logonParams[a].Value;
  239.                 }
  240.                 else if (String.Compare(logonParams[a].Name, "Guest Id") == 0)
  241.                 {
  242.                     guestId = logonParams[a].Value;
  243.                 }
  244.                 else if (String.Compare(logonParams[a].Name, "Guest Password") == 0)
  245.                 {
  246.                     guestPassword = logonParams[a].Value;
  247.                 }
  248.                 else if (String.Compare(logonParams[a].Name, "Server") == 0)
  249.                 {
  250.                     server = logonParams[a].Value;
  251.                 }
  252.                 else if (String.Compare(logonParams[a].Name, "Scope") == 0)
  253.                 {
  254.                     scope = logonParams[a].Value;
  255.                 }
  256.             }
  257.  
  258.             LogonReturnValues returnVal = new LogonReturnValues();
  259.  
  260.             if (String.IsNullOrEmpty(server) || String.IsNullOrEmpty(domain) ||
  261.                 String.IsNullOrEmpty(guestId) || String.IsNullOrEmpty(guestPassword))
  262.             {
  263.                 //Missing parameters
  264.                 returnVal.Response = (int)LogonErrors.MISSING_VALUES;
  265.                 return returnVal;
  266.             }
  267.  
  268.             //Logon successful
  269.             Ldap ldap = new Ldap(guestId, guestPassword, server, domain, scope);
  270.             //perform test
  271.             returnVal.Response = (int)ldap.checkPassword(userName, password);
  272.             if (returnVal.Response == (int)LogonErrors.NONE)
  273.             {
  274.                 returnVal.EmailAddress = ldap.returnEmailAddress();
  275.                 returnVal.FullName = ldap.returnFullName();
  276.                 returnVal.ResolvedUserName = ldap.returnResolvedAccountName();
  277.  
  278.                 //Determine what the domain is
  279.                 returnVal.Domain = generateDomain(domain);
  280.                
  281.                 storeParams.Add("HomeDirectory", ldap.returnHomeDirectory());
  282.                 storeParams.Add("DisplayName", ldap.returnDisplayName());
  283.                 storeParams.Add("AccountName", ldap.returnAccountName());
  284.                 storeParams.Add("DistinguishedName", ldap.returnDistinguishedName());
  285.             }
  286.  
  287.             return returnVal;
  288.         }
  289.  
  290.         private String generateDomain(String ldapValue)
  291.         {
  292.             String generatedDomain = "";
  293.  
  294.             try
  295.             {
  296.                 String[] vals = ldapValue.Split(new char[] { ',' });
  297.  
  298.                 for (int a = 0; a < vals.Length; a++)
  299.                 {
  300.                     if (!(vals[a] == null) && !(vals[a].Trim().Equals("")) &&
  301.                         vals[a].Substring(0, 3).Equals("DC=", StringComparison.CurrentCultureIgnoreCase))
  302.                     {
  303.                         if (vals[a].Length > 3)
  304.                         {
  305.                             generatedDomain += vals[a].Substring(3);
  306.  
  307.                             if (a != vals.Length - 1)
  308.                                 generatedDomain += ".";
  309.                         }
  310.                     }
  311.                 }
  312.             }
  313.             catch (Exception)
  314.             {
  315.                 generatedDomain = null;
  316.             }
  317.  
  318.             return generatedDomain;
  319.         }
  320.  
  321.         public String performSetupTest(Object parameters)
  322.         {
  323.             PropertiesClass props = (PropertiesClass) parameters;
  324.  
  325.             if (String.IsNullOrEmpty(props.Server) || String.IsNullOrEmpty(props.Server.Trim()))
  326.             {
  327.                 return String.Format(Strings.ReturnString("VALUE_NOT_SET"), Strings.ReturnString("SERVER"));
  328.             }
  329.             if (String.IsNullOrEmpty(props.Domain) || String.IsNullOrEmpty(props.Domain.Trim()))
  330.             {
  331.                 return String.Format(Strings.ReturnString("VALUE_NOT_SET"), Strings.ReturnString("DOMAIN"));
  332.             }
  333.             if (String.IsNullOrEmpty(props.UserId) || String.IsNullOrEmpty(props.UserId.Trim()))
  334.             {
  335.                 return String.Format(Strings.ReturnString("VALUE_NOT_SET"), Strings.ReturnString("GUEST_ID"));
  336.             }
  337.             if (String.IsNullOrEmpty(props.Password) || String.IsNullOrEmpty(props.Password.Trim()))
  338.             {
  339.                 return String.Format(Strings.ReturnString("VALUE_NOT_SET"), Strings.ReturnString("GUEST_PASSWORD"));
  340.             }
  341.             if (String.IsNullOrEmpty(props.Scope) || String.IsNullOrEmpty(props.Scope.Trim()))
  342.             {
  343.                 return String.Format(Strings.ReturnString("VALUE_NOT_SET"), Strings.ReturnString("SEARCH_SCOPE"));
  344.             }
  345.  
  346.             //#region test connection - bypasses test and returns sucess
  347.             loading = new LoadingDialog(Strings.ReturnString("PLEASE_WAIT"), String.Format(Strings.ReturnString("CHECKING_VALUE_SETTINGS"), "LDAP"), Strings.ReturnString("CANCEL"));
  348.  
  349.             LdapThread ldap = new LdapThread(props);
  350.             Thread thr = new Thread(new ThreadStart(ldap.ThreadTask));
  351.             thr.Start();
  352.  
  353.             if (loading.ShowDialog() == DialogResult.Abort)
  354.             {
  355.                 loading.Close();
  356.                 thr.Abort();
  357.                 return "-1";
  358.             }
  359.             else
  360.             {
  361.                 if (message.Equals(""))
  362.                 {
  363.                     return null;
  364.                 }
  365.                 else
  366.                 {
  367.                     return message;
  368.                 }
  369.             }
  370.             //#endregion
  371.             //return null;
  372.         }
  373.  
  374.         public void saveTestCredentials(DataSet dataSet, long index)
  375.         {
  376.             dataSet.Tables["Security Profiles Stored Logins"].Rows.Add(saveCredRow);
  377.         }
  378.  
  379.         class LdapThread
  380.         {
  381.             private PropertiesClass props = null;
  382.  
  383.             public LdapThread(PropertiesClass props)
  384.             {
  385.                 this.props = props;
  386.             }
  387.  
  388.             public void ThreadTask()
  389.             {
  390.                 Thread.Sleep(500);
  391.                 Ldap ldap = new Ldap(props.UserId, props.Password, props.Server, props.Domain, props.Scope);
  392.  
  393.                 message = ldap.checkServer();
  394.  
  395.                 if (loading.InvokeRequired)
  396.                     loading.Invoke(new MethodInvoker(loading.Close));
  397.                 else
  398.                     loading.Close();
  399.             }
  400.         }
  401.     }
  402. }
Advertisement
Add Comment
Please, Sign In to add comment