void ISecurityDescriptorCmdletProvider.GetSecurityDescriptor(string path, AccessControlSections includeSections)
        {
            string relative_path = GetRelativePath(PSPathToNT(path));

            using (NtDirectory dir = GetPathDirectory(relative_path))
            {
                if (relative_path.Length == 0)
                {
                    WriteItemObject(new GenericObjectSecurity(dir, includeSections), path, true);
                }
                else
                {
                    ObjectDirectoryInformation dir_info = GetEntry(dir, relative_path);
                    if (dir_info == null)
                    {
                        throw new NtException(NtStatus.STATUS_OBJECT_NAME_NOT_FOUND);
                    }

                    using (NtObject obj = dir_info.Open(GenericAccessRights.ReadControl))
                    {
                        WriteItemObject(new GenericObjectSecurity(obj, includeSections), path, obj is NtDirectory);
                    }
                }
            }
        }
        /// <summary>
        /// Overidden method to check if an item is a container.
        /// </summary>
        /// <param name="path">The drive path to check.</param>
        /// <returns>True if the item is a container.</returns>
        protected override bool IsItemContainer(string path)
        {
            bool is_container = false;

            if (GetDrive() == null)
            {
                return(false);
            }

            path = GetRelativePath(PSPathToNT(path));
            // The root always exists.
            if (path.Length == 0)
            {
                return(true);
            }

            try
            {
                using (NtDirectory dir = GetPathDirectory(path))
                {
                    ObjectDirectoryInformation dir_info = GetEntry(dir, path);
                    is_container = dir_info != null &&
                                   dir_info.TypeName.Equals("directory", StringComparison.OrdinalIgnoreCase);
                }
            }
            catch (NtException)
            {
            }

            return(is_container || GetDrive().DirectoryRoot.DirectoryExists(path));
        }
        /// <summary>
        /// Overridden method to get the item from a path.
        /// </summary>
        /// <param name="path">The drive path.</param>
        protected override void GetItem(string path)
        {
            if (GetDrive() == null)
            {
                return;
            }

            string relative_path = GetRelativePath(PSPathToNT(path));

            using (NtDirectory dir = GetPathDirectory(relative_path))
            {
                if (relative_path.Length == 0)
                {
                    WriteItemObject(new NtDirectoryEntry(GetDrive().DirectoryRoot, relative_path, String.Empty, "Directory"),
                                    NTPathToPS(BuildDrivePath(relative_path)), true);
                }
                else
                {
                    ObjectDirectoryInformation dir_info = GetEntry(dir, relative_path);
                    if (dir_info != null)
                    {
                        WriteItemObject(new NtDirectoryEntry(GetDrive().DirectoryRoot, relative_path, dir_info.Name, dir_info.TypeName),
                                        NTPathToPS(BuildDrivePath(relative_path)), dir_info.IsDirectory);
                    }
                }
            }
        }
        protected override void GetItem(string path)
        {
            if (GetDrive() == null)
            {
                return;
            }

            string normalized_path = NormalizePath(path);

            if (_item_cache.ContainsKey(normalized_path))
            {
                ObjectDirectoryEntry entry = _item_cache[normalized_path];
                WriteItemObject(entry, path, entry.IsDirectory);
            }
            else
            {
                using (NtDirectory dir = GetPathDirectory(path))
                {
                    ObjectDirectoryInformation dir_info = GetEntry(dir, path);
                    if (dir_info != null)
                    {
                        WriteItemObject(new ObjectDirectoryEntry(GetDrive().DirectoryRoot, normalized_path, dir_info.Name, dir_info.TypeName), path.TrimStart('\\'), dir_info.IsDirectory);
                    }
                }
            }
        }
        private NtResult <NtObject> OpenObject(ObjectDirectoryInformation entry, NtObject root, AccessMask desired_access)
        {
            NtType type = entry.NtType;

            using (var obja = new ObjectAttributes(entry.Name, AttributeFlags.CaseInsensitive, root))
            {
                return(type.Open(obja, desired_access, false));
            }
        }
        public void GetSecurityDescriptor(string path, AccessControlSections includeSections)
        {
            using (NtDirectory dir = GetPathDirectory(path))
            {
                ObjectDirectoryInformation dir_info = GetEntry(dir, path);
                if (dir_info == null)
                {
                    throw new NtException(NtStatus.STATUS_OBJECT_NAME_NOT_FOUND);
                }

                using (NtObject obj = dir_info.Open(GenericAccessRights.ReadControl))
                {
                    WriteItemObject(new GenericObjectSecurity(obj, includeSections), path, false);
                }
            }
        }
예제 #7
0
        void ISecurityDescriptorCmdletProvider.SetSecurityDescriptor(string path, ObjectSecurity securityDescriptor)
        {
            if (securityDescriptor is GenericObjectSecurity obj_security)
            {
                string relative_path = GetRelativePath(PSPathToNT(path));
                using (NtDirectory dir = GetPathDirectory(relative_path))
                {
                    ObjectDirectoryInformation dir_info = GetEntry(dir, relative_path);
                    if (dir_info == null)
                    {
                        throw new NtException(NtStatus.STATUS_OBJECT_NAME_NOT_FOUND);
                    }

                    using (NtObject obj = dir_info.Open(GenericAccessRights.WriteDac))
                    {
                        obj_security.PersistHandle(obj.Handle);
                    }
                }
            }
        }
        public void SetSecurityDescriptor(string path, ObjectSecurity securityDescriptor)
        {
            GenericObjectSecurity obj_security = securityDescriptor as GenericObjectSecurity;

            if (obj_security != null)
            {
                using (NtDirectory dir = GetPathDirectory(path))
                {
                    ObjectDirectoryInformation dir_info = GetEntry(dir, path);
                    if (dir_info == null)
                    {
                        throw new NtException(NtStatus.STATUS_OBJECT_NAME_NOT_FOUND);
                    }

                    using (NtObject obj = dir_info.Open(GenericAccessRights.WriteDac))
                    {
                        obj_security.PersistHandle(obj.Handle);
                    }
                }
            }
        }
        protected override bool IsItemContainer(string path)
        {
            bool is_container = false;
            bool is_symlink   = false;

            if (GetDrive() == null)
            {
                return(false);
            }

            path = NormalizePath(path);
            // The root always exists.
            if (path.Length == 0)
            {
                return(true);
            }

            try
            {
                using (NtDirectory dir = GetPathDirectory(path))
                {
                    ObjectDirectoryInformation dir_info = GetEntry(dir, path);
                    is_container = dir_info != null &&
                                   dir_info.TypeName.Equals("directory", StringComparison.OrdinalIgnoreCase);
                    is_symlink = dir_info != null &&
                                 dir_info.TypeName.Equals("symboliclink", StringComparison.OrdinalIgnoreCase);
                }
            }
            catch (NtException)
            {
            }

            //if (is_symlink && IsGlobalRoot(path))
            //{
            //    is_container = true;
            //}

            return(is_container || GetDrive().DirectoryRoot.DirectoryExists(path));
        }
예제 #10
0
        public void EnumObjects(EnumObjectsDelegate callback)
        {
            NtStatus status;
            int      context   = 0;
            bool     firstTime = true;
            int      retLength;

            using (var data = new MemoryAlloc(0x200))
            {
                while (true)
                {
                    while ((status = Win32.NtQueryDirectoryObject(
                                this,
                                data,
                                data.Size,
                                false,
                                firstTime,
                                ref context,
                                out retLength
                                )) == NtStatus.MoreEntries)
                    {
                        // Check if we have at least one entry. If not,
                        // we need to double the buffer size and try again.
                        if (data.ReadStruct <ObjectDirectoryInformation>(0).Name.Buffer != IntPtr.Zero)
                        {
                            break;
                        }

                        if (data.Size > 16 * 1024 * 1024)
                        {
                            Win32.ThrowLastError(status);
                        }

                        data.Resize(data.Size * 2);
                    }

                    if (status >= NtStatus.Error)
                    {
                        Win32.ThrowLastError(status);
                    }

                    int i = 0;

                    while (true)
                    {
                        ObjectDirectoryInformation info = data.ReadStruct <ObjectDirectoryInformation>(i);

                        if (info.Name.Buffer == IntPtr.Zero)
                        {
                            break;
                        }

                        if (!callback(new ObjectEntry(info.Name.Read(), info.TypeName.Read())))
                        {
                            return;
                        }

                        i++;
                    }

                    if (status != NtStatus.MoreEntries)
                    {
                        break;
                    }

                    firstTime = false;
                }
            }
        }
 internal NtObjectContainerEntry(ObjectDirectoryInformation dir_info)
     : this(dir_info.FullPath, dir_info.Name, dir_info.NtTypeName,
            dir_info.IsDirectory)
 {
 }