Guest User

OlderLDAP

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