예제 #1
0
        public static bool GetSamPathFromWinNTPath(
            string objPath,
            out string samPath
            )
        {
            Debug.Assert(!string.IsNullOrEmpty(objPath));
            Debug.Assert(IsWinntPath(objPath));

            // Init returns.
            bool isOk = true;

            samPath = null;

            // Construct pathname object and get num elements.
            ActiveDs.Pathname path = null;
            int numElements        = 0;

            if (isOk)
            {
                path = new ActiveDs.Pathname();
                path.Set(objPath, 1);//Constants.AdsSetTypeDn);

                // Get number of DN elements in the path.
                numElements = path.GetNumElements();
                if (numElements == 0)
                {
                    logX.loggerX.Error("ERROR - no elements in the LDAP path, ", objPath);
                    isOk = false;
                }
            }

            // Get domain and account.
            string domain  = string.Empty;
            string account = string.Empty;

            if (isOk)
            {
                switch (numElements)
                {
                case 1:
                    domain  = string.Empty;
                    account = path.GetElement(0);
                    break;

                case 2:
                    domain  = path.GetElement(1);
                    account = path.GetElement(0);
                    break;

                case 3:
                    domain  = path.GetElement(1);
                    account = path.GetElement(0);
                    break;

                default:
                    //Debug.Assert(false);
                    logX.loggerX.Error("ERROR - not valid number of elements in the path, ", objPath);
                    isOk = false;
                    break;
                }
            }

            // Create sam path.
            if (isOk)
            {
                samPath = MakeSamPath(domain, account);
            }

            return(isOk);
        }
예제 #2
0
        public static bool ExtractDomainFromPath(
            string objPath,
            out string domain,
            out bool isFlat
            )
        {
            Debug.Assert(!string.IsNullOrEmpty(objPath));

            // Init returns.
            domain = null;
            isFlat = false;
            bool isOk = true;

            // Get the path type.
            PathType type = getPathType(objPath);

            if (type == PathType.Unknown)
            {
                logX.loggerX.Error("ERROR - failed to detect path type");
                isOk = false;
            }

            // Construct pathname object and get num elements.
            ActiveDs.Pathname path = null;
            int numElements        = 0;

            if (isOk)
            {
                path = new ActiveDs.Pathname();
                path.Set(objPath, 1);//Constants.AdsSetTypeDn);

                // Get number of DN elements in the path.
                numElements = path.GetNumElements();
                if (numElements == 0)
                {
                    logX.loggerX.Error("ERROR - no elements in the LDAP path, ", objPath);
                    isOk = false;
                }
            }

            // Process based on the path type.
            if (isOk)
            {
                switch (type)
                {
                case PathType.AD:

                    // Parse pased on the type of path.
                    StringBuilder domBldr = new StringBuilder();
                    for (int i = 0; i < numElements; ++i)
                    {
                        // If the element has a dc attribute, take everything
                        // to the right of = sign.  No blank trimming is needed,
                        // because the pathname coclass handles it correctly.
                        string pathElement = path.GetElement(i);
                        if (string.Compare(Constants.LdapDcAttrib, 0,
                                           pathElement, 0, Constants.LdapDcAttrib.Length, true) == 0)
                        {
                            int eqIndex = pathElement.IndexOf('=');     // find = sign
                            if (eqIndex != -1)
                            {
                                domBldr.Append(pathElement.Substring(eqIndex + 1));     // take everything right of it
                                if (i != (numElements - 1))
                                {
                                    domBldr.Append(".");
                                }                                                        // append . if not last element
                            }
                        }
                    }

                    // Set the name type to non-flat (i.e. DNS), and get the DNS domain name.
                    isFlat = false;
                    domain = domBldr.ToString();
                    if (string.IsNullOrEmpty(domain))
                    {
                        logX.loggerX.Error("ERROR - failed to parse to get the DNS domain name for, ", objPath);
                        isOk = false;
                    }
                    break;

                case PathType.WinNT:
                    switch (numElements)
                    {
                    case 1:
                        domain = string.Empty;
                        break;

                    case 2:
                        domain = path.GetElement(1);
                        break;

                    case 3:
                        domain = path.GetElement(1);
                        break;

                    default:
                        //Debug.Assert(false);
                        logX.loggerX.Error("ERROR - not valid number of elements in the path, ", objPath);
                        isOk = false;
                        break;
                    }

                    if (isOk)
                    {
                        isFlat = true;
                        if (numElements != 1 && string.IsNullOrEmpty(domain))
                        {
                            logX.loggerX.Error("ERROR - failed to parse to get the Flat domain name for, ", objPath);
                            isOk = false;
                        }
                    }

                    break;

                case PathType.Unknown:
                default:
                    Debug.Assert(false);
                    break;
                }
            }

            return(isOk);
        }