示例#1
0
        public static int ModifyProperty(Likewise.LMC.LDAP.DirectoryContext dirContext, string nodeDN, string propertyName, PropertyValueCollection propertyValue)
        {
            List <object> valueObjects = propertyValue.ValueCollection;

            string[] values;

            if (valueObjects == null || valueObjects.Count == 0)
            {
                values = new string[] { null }
            }
            ;
            else if (valueObjects.Count == 1)
            {
                values = new string[] { ParsingValueObj(valueObjects[0]), null };
                //Console.WriteLine("In SDSUtils::modifyPropertyvalue is " + ParsingValueObj(valueObjects[0]));
            }
            else
            {
                values = new string[valueObjects.Count + 1];
                int i;
                for (i = 0; i < valueObjects.Count; i++)
                {
                    values[i] = ParsingValueObj(valueObjects[i]);
                    //Console.WriteLine("In SDSUtils::modifyPropertyvalue " + i + "is " + values[i]);
                }
                values[i] = null;
            }

            LDAPMod[] attrinfo = new LDAPMod[] { new LDAPMod((int)LDAPMod.mod_ops.LDAP_MOD_REPLACE, propertyName, values) };

            return(dirContext.ModifySynchronous(nodeDN, attrinfo));
        }
示例#2
0
        public static int DeleteObj(Likewise.LMC.LDAP.DirectoryContext dirContext, string nodeDN)
        {
            if (dirContext != null)
            {
                return(dirContext.DeleteSynchronous(nodeDN));
            }

            return(-1);
        }
示例#3
0
        //find the DN of given the sid
        public static string SearchBySid(string sid, Likewise.LMC.LDAP.DirectoryContext dirContext)
        {
            string searchFilter = string.Concat("(objectSid=", sid, ")");

            LdapMessage ldapMessage = dirContext.SearchSynchronous(
                dirContext.RootDN,
                LdapAPI.LDAPSCOPE.SUB_TREE,
                searchFilter,
                null,
                false);

            if (ldapMessage == null)
            {
                // Logger.Log("ldapMessage = null");
                return(null);
            }
            else
            {
                List <LdapEntry> ldapEntries = ldapMessage.Ldap_Get_Entries();
                if (ldapEntries == null || ldapEntries.Count == 0)
                {
                    // Logger.Log("ldapEntries.Count == 0");
                    return(null);
                }

                LdapEntry ldapNextEntry = ldapEntries[0];

                if (ldapNextEntry != null)
                {
                    string[] attrsList = ldapNextEntry.GetAttributeNames();

                    if (attrsList != null)
                    {
                        foreach (string attr in attrsList)
                        {
                            if (attr.Equals("distinguishedName", StringComparison.InvariantCultureIgnoreCase))
                            {
                                LdapValue[] attrValues = ldapNextEntry.GetAttributeValues(attr, dirContext);

                                if (attrValues != null && attrValues.Length > 0)
                                {
                                    return(attrValues[0].stringData);
                                }
                            }
                        }
                    }
                }
                return(null);
            }
        }
        public void SetData(DirectoryContext dirContext, IPlugIn plugin, IPlugInContainer container)
        {
            this.dirContext = dirContext;
            this.plugin = plugin;
            this.container = container;

            lvDomainOUs.Items.Clear();
            cbDomainOUs.Items.Add(dirContext.DomainName);
            cbDomainOUs.SelectedIndex = 0;
            ListOUChildren(dirContext.RootDN);
            sParentDN = dirContext.RootDN;
            SetNavState(false);
            RefreshlvItems();
        }
示例#5
0
        public static int AddNewObj(Likewise.LMC.LDAP.DirectoryContext dirContext, string choosenclass, string nodeDN)
        {
            if (dirContext != null)
            {
                LDAPMod[] info = new LDAPMod[1];

                string[] objectClass_values = new string[] { choosenclass, null };
                info[0] = new LDAPMod((int)LDAPMod.mod_ops.LDAP_MOD_ADD, "ObjectClass", objectClass_values);

                return(dirContext.AddSynchronous(nodeDN, info));
            }

            return(-1);
        }
示例#6
0
        //find the DN of given the groupsid
        public static string SearchByGuid(string objectGuid, DirectoryContext dirContext)
        {
            int ret = -1;
            string searchFilter = objectGuid;
            searchFilter = string.Concat("(objectGuid=", objectGuid, ")");
            List<LdapEntry> ldapEntries = null;

            ret = dirContext.ListChildEntriesSynchronous(
                               dirContext.RootDN,
                               LdapAPI.LDAPSCOPE.SUB_TREE,
                               searchFilter,
                               null,
                               false,
                               out ldapEntries);

            if (ldapEntries == null || ldapEntries.Count == 0)
            {
                Logger.Log("ldapEntries.Count == 0");
                return null;
            }

            LdapEntry ldapNextEntry = ldapEntries[0];

            if (ldapNextEntry != null)
            {
                string[] attrsList = ldapNextEntry.GetAttributeNames();

                if (attrsList != null)
                {
                    foreach (string attr in attrsList)
                    {
                        if (attr.Equals("distinguishedName", StringComparison.InvariantCultureIgnoreCase))
                        {
                            LdapValue[] attrValues = ldapNextEntry.GetAttributeValues(attr, dirContext);

                            if (attrValues != null && attrValues.Length > 0)
                            {
                                return attrValues[0].stringData;
                            }
                        }
                    }
                }
            }
            return null;
        }
示例#7
0
        //this function will return a ldapMessage that contains all the attributes that are available for an object
        //use this to populate DirectoryEntry's properties
        public static List <string> InitLdapMessageFilterForProperties(Likewise.LMC.LDAP.DirectoryContext dirContext, string nodeDN)
        {
            LdapMessage ldapMessagetemp = null;

            string[] attrs = { "name", "allowedAttributes", null };

            if (ldapMessagetemp == null)
            {
                ldapMessagetemp = dirContext.SearchSynchronous(
                    nodeDN,
                    LdapAPI.LDAPSCOPE.BASE,
                    "(objectClass=*)",
                    attrs,
                    false);
            }

            if (ldapMessagetemp == null)
            {
                return(null);
            }

            List <LdapEntry> ldapEntries = ldapMessagetemp.Ldap_Get_Entries();

            if (ldapEntries == null || ldapEntries.Count == 0)
            {
                return(null);
            }

            LdapEntry ldapNextEntry = ldapEntries[0];

            List <string> allowedAttributes = new List <string>();

            LdapValue[] attrValues = ldapNextEntry.GetAttributeValues("allowedAttributes", dirContext);
            if (attrValues != null && attrValues.Length > 0)
            {
                foreach (LdapValue attrValue in attrValues)
                {
                    allowedAttributes.Add(attrValue.stringData);
                }
            }

            return(allowedAttributes);
        }
 /// <summary>
 /// initializes the Directory node with the selected AD Object DistinguishedName,Objectclass and directory context objects
 /// initializes selected pulgin info
 /// </summary>
 /// <param name="distinguishedName"></param>
 /// <param name="dirContext"></param>
 /// <param name="objectClass"></param>
 /// <param name="image"></param>
 /// <param name="t"></param>
 /// <param name="plugin"></param>
 public ADUCDirectoryNode(string distinguishedName,
 DirectoryContext dirContext,   
 string objectClass,
 Icon image,
 Type t,
 IPlugIn plugin,
 bool IsDisabled)
 : base(distinguishedName, image, t, plugin)
 {
     
     this.distinguishedName = distinguishedName;
     this.dirContext = dirContext;      
     sc = this.sc;
     
     _IsDisabled = IsDisabled;
     
     if (!distinguishedName.StartsWith("DC=", StringComparison.InvariantCultureIgnoreCase))
     {
         string[] parts = distinguishedName.Split(',');
         if (parts.Length > 0)
         {
             Text = parts[0];
         }
     }
     
     _objectClass = objectClass;
     
     if (_IsDisabled && _objectClass.Equals("user", StringComparison.InvariantCultureIgnoreCase))
     {
         objectClass = "disabledUser";
     }
     else if (_IsDisabled && _objectClass.Equals("computer", StringComparison.InvariantCultureIgnoreCase))
     {
         objectClass = "disabledComputer";
     }
     
     ImageIndex = (int)GetNodeType(objectClass);
     SelectedImageIndex = (int)GetNodeType(objectClass);
 }
        //Function to create domain level dirContext object
        public static DirectoryContext CreateDirectoryContext(
                                        string DomainControllerName,
                                        string rootDN,
                                        string UserName,
                                        string Password,
                                        int portNumber,
                                        bool usingSimpleBind,
                                        out string errorMessage)
        {
            int ret = -1;

            errorMessage = null;

            string sDomainControllerIP = null;
            IPHostEntry domainControllerEntry=null;

            try
            {
                if (DomainControllerName != null)
                {
                    domainControllerEntry = Dns.GetHostEntry(DomainControllerName);
                }
                else
                {
                    return null;
                }
            }
            catch(Exception ex)
            {
                errorMessage =
                    String.Format(
                    "The specified domain either does not exist or DNS could not resolve the address of the domain controller : {0}",
                    DomainControllerName);
                Logger.Log("DirectoryContext.CreateDirectoryContext():" + ex.Message);
                Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
                //Logger.ShowUserError(errorMessage);
                return null;
            }

            if (domainControllerEntry != null && domainControllerEntry.AddressList.Length > 0)
            {
                sDomainControllerIP = domainControllerEntry.AddressList[0].ToString();
            }
            else
            {
                errorMessage =
                    String.Format(
                    "DirectoryContext.CreateDirectoryContext(): Could not resolve address of domain controller : {0}",
                    DomainControllerName);
                //Logger.ShowUserError("DirectoryContext.CreateDirectoryContext(): Could not resolve address of domain controller: {0}");
                Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
                errorMessage = "";
                return null;
            }

            IntPtr ld = LdapAPI.ldap_open(sDomainControllerIP, portNumber);
            if (ld == IntPtr.Zero)
            {
                errorMessage =
                    "The specified domain either does not exist or could not be contacted.  " +
                    "Please contact your system administrator to verify that your domain is " +
                    "properly configured and is currently online.";
                Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
                errorMessage = "";
                //Logger.ShowUserError(errorMessage);
                return null;
            }

            //LdapTimeVal timeout = new LdapTimeVal(1, 0);
            ////IntPtr ptrTimeout = IntPtr.Zero;
            ////ptrTimeout = Marshal.AllocHGlobal(Marshal.SizeOf(timeout));
            ////Marshal.WriteInt32(ptrTimeout, timeout.);
            //ret = LdapAPI.ldap_connect(ld, timeout.ConvertToUM());
            //if (BailOnLdapError("LdapAPI.ldap_connect :", ret, out errorMessage))
            //{
            //    Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
            //    errorMessage = "";
            //    return null;
            //}

            LdapHandle ldapHandle = new LdapHandle(ld);

            string distinguishedName = string.Format("cn={0},cn=Users,{1}", UserName, rootDN);
            if (!String.IsNullOrEmpty(UserName) && !UserName.Equals("administrator", StringComparison.InvariantCultureIgnoreCase))
                distinguishedName = UserName;

            int version = LDAP_VERSION3;
            IntPtr ptrVersion = IntPtr.Zero;
            ptrVersion = Marshal.AllocHGlobal(Marshal.SizeOf(version));
            Marshal.WriteInt32(ptrVersion, version);
            ret = LdapAPI.ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, ptrVersion);
            if(BailOnLdapError("ldap_set_option OPT_PROTOCOL_VERSION :", ret, out errorMessage))
            {
                Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
                errorMessage = "";
                return null;
            }

            ret = LdapAPI.ldap_set_option(ld, LDAP_OPT_REFERRALS, IntPtr.Zero);
            if(BailOnLdapError("ldap_set_option LDAP_OPT_REFERRALS :", ret, out errorMessage))
            {
                Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
                errorMessage = "";
                return null;
            }

            if (!String.IsNullOrEmpty(Password))
            {
                usingSimpleBind = Configurations.SSOFailed = true;
            }

            if (usingSimpleBind)
            {
                if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows)
                {
                    ret = LdapAPI.ldap_bind_s(ld, distinguishedName, Password, (ulong)LDAP_AUTHWindows.LDAP_AUTH_SIMPLE);
                }
                else
                {
                    ret = LdapAPI.ldap_bind_s(ld, distinguishedName, Password, (ulong)LDAP_AUTHLinux.LDAP_AUTH_SIMPLE);
                }
                if (BailOnLdapError("", ret, out errorMessage))
                {
                    Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
                    return null;
                }
            }
            else  /***GSS-bind***/
            {
                if (Configurations.currentPlatform != LikewiseTargetPlatform.Windows)
                {
                    //set GSS-Remote Principal Name

                    //char ber_pvt_opt_on;    /* used to get a non-NULL address for *_OPT_ON */
                    ///* option on/off values */
                    //#define LDAP_OPT_ON           ((void *) &ber_pvt_opt_on)
                    //#define LDAP_OPT_OFF    ((void *) 0)
                    //status = ldap_set_option(ld, LDAP_OPT_X_GSSAPI_ALLOW_REMOTE_PRINCIPAL, LDAP_OPT_ON);

                    char ber_pvt_opt_on = '1';
                    IntPtr ptrRemotePrincipalflags = IntPtr.Zero;
                    ptrRemotePrincipalflags = Marshal.AllocHGlobal(Marshal.SizeOf(ber_pvt_opt_on));
                    Marshal.WriteInt32(ptrRemotePrincipalflags, ber_pvt_opt_on);
                    ret = LdapAPI.ldap_set_option(ld, LDAP_OPT_X_GSSAPI_ALLOW_REMOTE_PRINCIPAL, ptrRemotePrincipalflags);
                    if (BailOnLdapError("DirectoryContext GSS-Bind: ldap_set_option LDAP_OPT_X_GSSAPI_ALLOW_REMOTE_PRINCIPAL, ptrRemotePrincipalflags=" + ptrRemotePrincipalflags,
                    ret, out errorMessage))
                    {
                        Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
                        errorMessage = "";
                        return null;
                    }
                }

                //set GSS-SPNEGO options
                int secflags = ISC_REQ_MUTUAL_AUTH | ISC_REQ_REPLAY_DETECT;

                IntPtr ptrSecflags = IntPtr.Zero;
                ptrSecflags = Marshal.AllocHGlobal(Marshal.SizeOf(secflags));
                Marshal.WriteInt32(ptrSecflags, secflags);
                ret = LdapAPI.ldap_set_option(ld, LDAP_OPT_SSPI_FLAGS, ptrSecflags);
                if (BailOnLdapError("DirectoryContext GSS-Bind: ldap_set_option LDAP_OPT_SSPI_FLAGS, secflags=" + secflags,
                    ret, out errorMessage))
                {
                    Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
                    errorMessage = "";
                    return null;
                }

                if (Configurations.currentPlatform != LikewiseTargetPlatform.Windows)
                {
                    secflags |= ISC_REQ_INTEGRITY;

                    ptrSecflags = IntPtr.Zero;
                    ptrSecflags = Marshal.AllocHGlobal(Marshal.SizeOf(secflags));
                    Marshal.WriteInt32(ptrSecflags, secflags);
                    ret = LdapAPI.ldap_set_option(ld, LDAP_OPT_SSPI_FLAGS, ptrSecflags);
                    if (BailOnLdapError("DirectoryContext GSS-Bind: ldap_set_option LDAP_OPT_SSPI_FLAGS, secflags=" + secflags,
                        ret, out errorMessage))
                    {
                        Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
                        errorMessage = "";
                        return null;
                    }

                    secflags |= ISC_REQ_CONFIDENTIALITY;

                    ptrSecflags = IntPtr.Zero;
                    ptrSecflags = Marshal.AllocHGlobal(Marshal.SizeOf(secflags));
                    Marshal.WriteInt32(ptrSecflags, secflags);
                    ret = LdapAPI.ldap_set_option(ld, LDAP_OPT_SSPI_FLAGS, ptrSecflags);
                    if (BailOnLdapError("DirectoryContext GSS-Bind: ldap_set_option LDAP_OPT_SSPI_FLAGS, secflags=" + secflags,
                        ret, out errorMessage))
                    {
                        Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
                        errorMessage = "";
                        return null;
                    }
                }
                if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows)
                {
                    ret = LdapAPI.ldap_bind_s(ld, null, null, (ulong)LDAP_AUTHWindows.LDAP_AUTH_NEGOTIATE);
                }
                else
                {
                    ret = LdapAPI.ldap_bind_s(ld, null, null, (ulong)LDAP_AUTHLinux.LDAP_AUTH_NEGOTIATE);
                }
                if (BailOnLdapError("DirectoryContext GSS-Bind:ldap_bind_s :", ret, out errorMessage))
                {
                    Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
                    errorMessage = "";
                    return null;
                }
            }

            //try to figure out the DC that serves as GC
            string configurationName = null;
            //searching with baseDn="" allows ldap to access the domain “RootDSE”.
            //Without passing that, it cannot access the configurationNamingContext
            DirectoryContext dirContext = new DirectoryContext(
                                DomainControllerName,
                                rootDN,
                                distinguishedName,
                                UserName,
                                Password,
                                ldapHandle,
                                portNumber);

            dirContext._domainControllerIP = sDomainControllerIP;
            LdapMessage ldapMessage;
            string gcServer = DomainControllerName;

            Logger.Log("root query is started querying ", Logger.ldapLogLevel);
            IntPtr MessagePtr;
            ret = LdapAPI.ldap_search_s(ld,
                                        "",
                                        (int)LdapAPI.LDAPSCOPE.BASE,
                                        "(objectClass=*)",
                                        null,
                                        0,
                                        out MessagePtr);

            ldapMessage = new LdapMessage(ldapHandle, MessagePtr);
            Logger.Log("root query is finished querying " + ret, Logger.ldapLogLevel);

            List<LdapEntry> ldapEntries = (ldapMessage != null ? ldapMessage.Ldap_Get_Entries() : null);
            Logger.Log("root query is finished querying with ldapEntries count : " + ldapEntries, Logger.ldapLogLevel);

            #region //Obtaining RootDSE attributes
            if (ldapEntries != null && ldapEntries.Count > 0)
            {
                LdapEntry rootDseEntry = ldapEntries[0];

                LdapValue[] values = rootDseEntry.GetAttributeValues("defaultNamingContext", dirContext);

                if (values != null && values.Length > 0)
                {
                    dirContext.DefaultNamingContext = values[0].stringData;
                }
                Logger.Log("defaultNamingContext is " + dirContext.DefaultNamingContext, Logger.ldapLogLevel);

                values = rootDseEntry.GetAttributeValues("schemaNamingContext", dirContext);

                if (values != null && values.Length > 0)
                {
                    dirContext.SchemaNamingContext = values[0].stringData;
                }
                Logger.Log("schemaNamingContext is " + dirContext.SchemaNamingContext, Logger.ldapLogLevel);

                values = rootDseEntry.GetAttributeValues("dnsHostName", dirContext);

                if (values != null && values.Length > 0)
                {
                    dirContext.DnsHostName = values[0].stringData;
                }
                Logger.Log("dnsHostName is " + dirContext.DnsHostName, Logger.ldapLogLevel);

                values = rootDseEntry.GetAttributeValues("rootDomainNamingContext", dirContext);

                if (values != null && values.Length > 0)
                {
                    dirContext.RootDomainNamingContext = values[0].stringData;
                }
                Logger.Log("dnsHostName is " + dirContext.RootDomainNamingContext, Logger.ldapLogLevel);

                values = rootDseEntry.GetAttributeValues("configurationNamingContext", dirContext);

                if (values != null && values.Length > 0)
                {
                    configurationName = values[0].stringData;
                }
                Logger.Log(
                    "configurationNamingContext is " + configurationName,
                    Logger.ldapLogLevel);

                values = rootDseEntry.GetAttributeValues("SupportedSASLMechanisms", dirContext);

                if (values != null && values.Length > 0)
                {
                    _supportedSASLMechanisms = new string[values.Length];
                    int index = 0;

                    foreach (LdapValue value in values)
                    {
                        _supportedSASLMechanisms[index] = value.stringData;

                        Logger.Log(
                                    "SupportedSASLMechanisms is " + value.stringData,
                                    Logger.ldapLogLevel);
                        index++;
                    }
                }

                dirContext.ConfigurationNamingContext = configurationName;

            #endregion

                ret = LdapAPI.ldap_search_s(ld,
                                        configurationName,
                                        (int)LdapAPI.LDAPSCOPE.BASE,
                                        "(&(objectcategory=ntdsdsa)(options=1))",
                                        new string[] { "distinguishedName", null },
                                        0,
                                        out MessagePtr);

                ldapMessage = new LdapMessage(ldapHandle, MessagePtr);

                if(BailOnLdapError("ldap_search_s :", ret, out errorMessage))
                {
                    Logger.Log("DirectoryContext.CreateDirectoryContext(): " + errorMessage);
                    errorMessage = "";
                    return null;
                }

                ldapEntries = (ldapMessage != null ? ldapMessage.Ldap_Get_Entries() : null);

                if (ldapEntries != null && ldapEntries.Count > 0)
                {
                    //we only check the first one, then we quit finding others (when we do optimization, this algorithm shall be a lot more complicated
                    LdapEntry ldapNextEntry = ldapEntries[0];

                    values = ldapNextEntry.GetAttributeValues("distinguishedName", dirContext);

                    if (values != null && values.Length > 0)
                    {
                        string dn = values[0].stringData;
                        string[] splits1 = dn.Split(',');
                        string[] splits2 = rootDN.Split(',');

                        gcServer = splits1[1].Substring(3).ToLower();
                        foreach (string str in splits2)
                            gcServer = string.Concat(gcServer, ".", str.Substring(3).ToLower());

                        Logger.Log(
                            "global catelog server is " + gcServer,
                            Logger.ldapLogLevel);
                    }
                }
            }

            dirContext.GCServername = gcServer;
            dirContext.BindMethod = usingSimpleBind;

            ldapHandle.DirectoryContext = dirContext;

            return dirContext;
        }
示例#10
0
        public LdapMessage(LdapHandle ldapHandle, IntPtr ldapMessage)
		{
			_ldapHandle = ldapHandle;
			_ldapMessage = ldapMessage;
		    _dircontext = null;
        }
示例#11
0
 public LdapHandle(IntPtr ld, DirectoryContext dircontext)
     : this()
 {
     _ld = ld;
     dirContext = dircontext;
 }
示例#12
0
        public LdapValue[] GetAttributeValues(IntPtr ldapEntry, string attrName, DirectoryContext dirContext)
        {

            if(dirContext == null ||
                String.IsNullOrEmpty(attrName))
            {
                return null;
            }

            if (dirContext.SchemaCache == null)
            {
                return GetNonBervals(ldapEntry, attrName);
            }

            LDAPSchemaCache schema = dirContext.SchemaCache;
            SchemaType foundType = schema.GetSchemaTypeByDisplayName(attrName);

            // if cannot find type in schemaCache, get byte[] first
            //(sometimes schemaCache doesn't contain objectGUID,...type information)
            if (foundType == null)
            {
                if (String.Equals(attrName, "objectGUID", StringComparison.InvariantCultureIgnoreCase) ||
                    String.Equals(attrName, "objectSid", StringComparison.InvariantCultureIgnoreCase) ||
                    String.Equals(attrName, "nTsecurityDescriptor", StringComparison.InvariantCultureIgnoreCase))
                {
                    return ReformatBervalsInHex(ldapEntry, attrName);
                }
                else
                {
                    return GetNonBervals(ldapEntry, attrName);
                }
            }
            ADSType adstype = foundType.DataType;

            if (adstype == ADSType.ADSTYPE_OCTET_STRING ||
                adstype == ADSType.ADSTYPE_NT_SECURITY_DESCRIPTOR)
            {
                return ReformatBervalsInHex(ldapEntry, attrName);
            }
            else if (adstype == ADSType.ADSTYPE_INTEGER ||
                    adstype == ADSType.ADSTYPE_LARGE_INTEGER)
            {
                LdapValue[] values = GetBervals(ldapEntry, attrName);

                if (values == null || values.Length == 0)
                {
                    return values;
                }
                foreach (LdapValue value in values)
                {

                    string replace = value.stringData.Replace("-", "");
                    char[] intValuechrs = new char[replace.Length / 2];

                    for (int i = 0; i < replace.Length / 2; i++)
                    {
                        intValuechrs[i] = replace[i * 2 + 1];
                    }

                    for (int i = 0; i < intValuechrs.Length; i++)
                    {
                        if (intValuechrs[i] < '0' || intValuechrs[i] > '9')
                        {
                            intValuechrs[i] = '-';
                        }
                    }
                    value.stringData = new string(intValuechrs);
                    value.stringData.Replace("-", "");

                    if (adstype == ADSType.ADSTYPE_INTEGER)
                    {
                        value.intData = Convert.ToInt32(value.stringData);
                        value.AdsType = ADSType.ADSTYPE_INTEGER;
                    }
                    else if (adstype == ADSType.ADSTYPE_LARGE_INTEGER)
                    {
                        value.longData = Convert.ToInt64(value.stringData);
                        value.AdsType = ADSType.ADSTYPE_LARGE_INTEGER;
                    }
                }
            }
            return GetNonBervals(ldapEntry, attrName);

        }
示例#13
0
        private DirectoryContext Assign_dirContext()
        {
            if (dirContext == null)
            {
                if (sProtocol.Equals("GC", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (!findDircontext(GC_PORT))
                    {
                        //Console.WriteLine("****************Creating GC directoryContext*******************");

                        if (findGCServer() != null)
                            sServer = findGCServer();

                        string errorMessage = null;

                        dirContext = DirectoryContext.CreateDirectoryContext(
                                        sServer,
                                        rootDN,
                                        this.sUsername,
                                        this.sPassword,
                                        GC_PORT,
                                        findBindingMethod(),
                                        out errorMessage);

                        if (dirContext != null)
                        {
                            //Console.WriteLine("-------------Global catalog DirectoryContext is created.--------------------");
                            exisitngDirContext.Add(dirContext);
                            if (!FindSchemaCache())
                            {
                                dirContext.SchemaCache = LDAPSchemaCache.Build(dirContext);
                                existingSchemaCache.Add(dirContext.SchemaCache);
                            }
                        }
                    }
                }
                else if (sProtocol.Equals("LDAP", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (!findDircontext(LDAP_PORT))
                    {
                        string errorMessage = null;

                        //Console.WriteLine("****************Creating Normal Ldap directoryContext*******************");
                        dirContext = DirectoryContext.CreateDirectoryContext(
                                        sServer,
                                        rootDN,
                                        this.sUsername,
                                        this.sPassword,
                                        LDAP_PORT,
                                        findBindingMethod(),
                                        out errorMessage);

                        if (dirContext != null)
                        {
                            //Console.WriteLine("-------------------Normal LDAP DirectoryContext is created.--------------------");
                            exisitngDirContext.Add(dirContext);
                            //Console.WriteLine("dircontext portnumber is " + dirContext.PortNumber + dirContext.LdapHandle);
                            if (!FindSchemaCache())
                            {
                                dirContext.SchemaCache = LDAPSchemaCache.Build(dirContext);
                                existingSchemaCache.Add(dirContext.SchemaCache);
                            }
                        }
                    }
                }
            }

            if (dirContext == null)
                throw new Exception("Create DirectoryContext Failed - logon failure: Unknown Username or Bad password.");
            else
                return dirContext;
        }
示例#14
0
        private bool findDircontext(int portNumber)
        {
            bool FindDirContext = false;

            if (exisitngDirContext.Count > 0)
            {
                foreach (DirectoryContext context in exisitngDirContext)
                {
                    if (HasCreds())
                    {
                        if (context.RootDN.Equals(rootDN, StringComparison.InvariantCultureIgnoreCase)
                         && context.PortNumber == portNumber
                         && context.UserName == this.Username
                         && context.Password == this.Password)
                        {
                            dirContext = context;
                            FindDirContext = true;
                            break;
                        }
                    }
                    else
                    {
                        if (context.RootDN.Equals(rootDN, StringComparison.InvariantCultureIgnoreCase)
                         && context.PortNumber == portNumber)
                        {
                            dirContext = context;
                            FindDirContext = true;
                            break;
                        }
                    }
                }
            }

            return FindDirContext;
        }
        public DirectoryContext(DirectoryContext dirContext)
        {

            _rootDN = dirContext.RootDN;

            string[] domainDNcom = _rootDN.Split(',');

            string domainName = "";
            foreach (string str in domainDNcom)
            {
                string temp = string.Concat(str.Substring(3), ".");
                domainName = string.Concat(domainName, temp);
            }

            domainName = domainName.Substring(0, domainName.Length - 1);

            _domainName = domainName;

            _domainControllerName = dirContext.DomainControllerName;
            _distinguishedName = dirContext.DistinguishedName;
            _userName = dirContext.UserName;
            _password = dirContext.Password;
            _ldapHandle = new LdapHandle(dirContext.LdapHandle.Handle, this);
            _portNumber = dirContext.PortNumber;
        }
 public static ADUCDirectoryNode GetDirectoryRoot(DirectoryContext dirCtx, string DistinguishedName, Icon image, Type t, IPlugIn plugin)
 {
     if (dirCtx == null)
     {
         Logger.LogMsgBox("DirectoryNode.GetDirectoryRoot(): dirCtx == null");
         return null;
     }
     
     ADUCDirectoryNode dn = new ADUCDirectoryNode(DistinguishedName,
     dirCtx, "container", image, t, plugin, false);
     
     return dn;
 }
示例#17
0
 //ldap_get_values returns char**; this must be manually marshaled
 public LdapValue[] GetAttributeValues(string attrName, DirectoryContext dirContext)
 {
     return _ldapMessage.GetAttributeValues(_ldapEntry, attrName, dirContext);
 }
示例#18
0
    /// <summary>
    /// Updates the listview with newly added/modified/deleted object on right hand side of the ADUCPlugin
    /// </summary>
    /// <param name="objectClass"></param>
    /// <param name="dn"></param>
    /// <param name="dirContext"></param>
    /// <param name="dirnode"></param>
    private void listViewhelper(string objectClass, string dn, DirectoryContext dirContext, ADUCDirectoryNode dirnode)
    {
        ADUCDirectoryNode dtn = new ADUCDirectoryNode(dn, dirContext, objectClass,
        Resources.Group_16, dirnode.NodeType, this.pi, dirnode.IsDisabled);

        ListViewItem lviArr;

        string[] parts = dtn.DistinguishedName.Split(',');
        string[] values = { parts.Length > 0 ? parts[0] : "", dtn.ObjectClass, dtn.DistinguishedName };

        lviArr = new ListViewItem(values);
        lviArr.Tag = dtn;
        lviArr.ImageIndex = (int)ADUCDirectoryNode.GetNodeType(dtn);

        lvChildNodes.Items.Add(lviArr);

    }
示例#19
0
 public static bool FindDirectoryContext(int portNumber, string rdn, out DirectoryContext dc)
 {
     if (exisitngDirContext.Count > 0)
     {
         foreach (DirectoryContext context in exisitngDirContext)
         {
             if (context.RootDN.Equals(rdn, StringComparison.InvariantCultureIgnoreCase)
                 && (context.PortNumber == portNumber))
             {
                 dc = context;
                 return true;
             }
         }
         dc = null;
         return false;
     }
     else
     {
         dc = null;
         return false;
     }
 }
示例#20
0
    /// <summary>
    /// Will be called when we click on 'Set Target Machine Info' menu item and if plugin is already connected to domain
    /// Will gets all nodes along with its childs
    /// </summary>
    private bool ConnectToDomain()
    {
        Logger.Log("ADUCPlugin.ConnectToDomain", Logger.manageLogLevel);
        
        if (_usingSimpleBind &&
        (!Hostinfo.HasCreds(_hn) || _hn.creds.Invalidated))
        {
            _container.ShowError("ADUC cannot connect to domain due to invalid credentials");
            _hn.IsConnectionSuccess = false;
            return _hn.IsConnectionSuccess;
        }

        //Make sure one ADUC plugin only hosts one domain

        if (_aducPagelvChildNodes != null)
        {
            _aducPagelvChildNodes.Items.Clear();
        }

        if (_pluginNode != null && _pluginNode.Nodes.Count != 0)
        {
            foreach (TreeNode treeNode in _pluginNode.Nodes)
            {
                LACTreeNode lacTreeNode = treeNode as LACTreeNode;
                if (lacTreeNode != null && lacTreeNode is LACTreeNode)
                {
                    if (!lacTreeNode._IsPlugIn)
                    {
                        _pluginNode.Nodes.Remove(treeNode);
                    }
                    else
                    {
                        ListViewItem lvItem = new ListViewItem(treeNode.Text);
                        lvItem.Tag = treeNode;
                        lvItem.ImageIndex = (int)treeNode.ImageIndex;
                        _aducPagelvChildNodes.Items.Add(lvItem);
                    }
                }
            }
        }
        
        _currentDomain = new ADUCDomain();
        _currentDomain.HostInfo = _hn;
        _currentDomain.plugin = this;
        
        if (_currentDomain.ConnectToDomain(_usingSimpleBind) != null)
        {
            if (!_domainList.ContainsKey(_currentDomain.rootDN.ToLower()))
            {
                _domainList.Add(_currentDomain.rootDN.ToLower(), _currentDomain);
            }
            
            _pluginNode.Text = string.Format("Active Directory Users & Computers [{0}]", _hn.domainName);
            
            if (_currentDomain.rootNode != null)
            {
                _currentDomain.rootNode.sc = _pluginNode.sc;
                //_pluginNode.Nodes.Add(domain.rootNode);
                bool IsFound = false;
                if (_pluginNode != null && _pluginNode.Nodes.Count != 0)
                {
                    int index = 0;
                    foreach (TreeNode treeNode in _pluginNode.Nodes)
                    {
                        IsFound = false;
                        ADUCDirectoryNode node = treeNode as ADUCDirectoryNode;
                        if (node != null && node.Name.Trim().ToLower().Equals(_currentDomain.rootNode.Name.Trim().ToLower()))
                        {
                            IsFound = true;
                            _pluginNode.Nodes.RemoveAt(index);
                            _pluginNode.Nodes.Insert(index, _currentDomain.rootNode);
                            break;
                        }
                        index++;
                    }
                }
                if (!IsFound)
                {
                    _pluginNode.Nodes.Add(_currentDomain.rootNode);
                }
                
                _currentDomain.rootNode.Collapse();
                _currentDomain.rootNode.ExpandAll();
            }
            
            _adContext = _currentDomain.adContext;
            _rootDN = _currentDomain.rootDN;
            _shortDomainName = _currentDomain.ShortDomainName;
            
            _defaultUnixHomedir = _currentDomain.DefaultUnixHomeDir;
            
            _loginShell = _currentDomain.LoginShell;          
            _hn.IsConnectionSuccess = true;
        }
        else
        {            
            _hn.IsConnectionSuccess = false;
        }
        
        return _hn.IsConnectionSuccess;
    }
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            LACTreeNode PluginNode = plugin.GetPlugInNode();
            LACTreeNode rootDNnode = PluginNode.Tag as LACTreeNode;
            dirContext = rootDNnode.Tag as DirectoryContext;

            ObjectsListPage objectsPage = new ObjectsListPage();
            objectsPage.SetData(dirContext, plugin, container);
            if (objectsPage.ShowDialog(this) == DialogResult.OK)
            {
                lblGPO.Text = objectsPage.GPODisplayName;
                Wizard.enableButton(WizardDialog.WizardButton.Finish);
                objectSelectDlg.displayName = objectsPage.GPODisplayName;
                objectSelectDlg.distinguishedName = objectsPage.DistinguishedName;
            }
        }
示例#22
0
    /// <summary>
    /// Method that is used to connect to multiple domains
    /// </summary>
    /// <returns></returns>
    public DirectoryContext ConnectToDomain(bool usingSimpleBind)
    {
        
        string[] rootDNcom = _hn.domainName.Split('.');
        
        string rootDN = "";
        foreach (string str in rootDNcom)
        {
            string temp = string.Concat("dc=", str, ",");
            rootDN = string.Concat(rootDN, temp);
        }
        
        rootDN = rootDN.Substring(0, rootDN.Length - 1);
        
        _rootDN = rootDN;
        
        if (DirectoryEntry.exisitngDirContext != null && DirectoryEntry.exisitngDirContext.Count > 0)
        {
            foreach (DirectoryContext dirContext in DirectoryEntry.exisitngDirContext)
            {
                if (dirContext.DistinguishedName.ToLower().Contains(rootDN.ToLower()))
                {
                    DirectoryEntry.exisitngDirContext.Remove(dirContext);
                    break;
                }
            }
        }
        
        if (DirectoryEntry.existingSchemaCache != null && DirectoryEntry.existingSchemaCache.Count > 0)
        {
            foreach (LDAPSchemaCache ldapSchema in DirectoryEntry.existingSchemaCache)
            {
                if (ldapSchema != null && ldapSchema.rootDN.Equals(rootDN, StringComparison.InvariantCultureIgnoreCase))
                {
                    DirectoryEntry.existingSchemaCache.Remove(ldapSchema);
                    break;
                }
            }
        }

        string errorMessage = null;

        Logger.Log(String.Format("ADUCDomain: About to build directory context: {0}", _hn.ToString()));

        _adContext = DirectoryContext.CreateDirectoryContext(_hn.domainControllerName,
        //_hn.domainName,
        rootDN,
        _hn.creds.UserName,
        _hn.creds.Password,
        389,
        usingSimpleBind,
        out errorMessage);

        if (String.IsNullOrEmpty(errorMessage))
        {
            Logger.Log("ADUCDomain: Built directory context", Logger.ADUCLogLevel);
        }
        else
        {
            Logger.ShowUserError(errorMessage);
        }

        if (_adContext != null && !BuildSchemaCache(usingSimpleBind))
        {
            Logger.Log("BuildSchemaCache() failed!");
        }
        
        return _adContext;
    }