private void SetSecurityDescriptor(string path, ObjectSecurity sd, AccessControlSections sections)
        {
            var currentPrivilegeState = new PlatformInvokes.TOKEN_PRIVILEGE();

            byte[] securityDescriptorBinary = null;

            try
            {
                // Get the binary form of the descriptor.
                PlatformInvokes.EnableTokenPrivilege("SeBackupPrivilege", ref currentPrivilegeState);
                securityDescriptorBinary = sd.GetSecurityDescriptorBinaryForm();
            }
            finally
            {
                PlatformInvokes.RestoreTokenPrivilege("SeBackupPrivilege", ref currentPrivilegeState);
            }

            try
            {
                PlatformInvokes.EnableTokenPrivilege("SeRestorePrivilege", ref currentPrivilegeState);

                // Transfer it to the new file / directory.
                // We keep these two code branches so that we can have more
                // granular information when we ouput the object type via
                // WriteSecurityDescriptorObject.
                if (Directory.Exists(path))
                {
                    DirectorySecurity newDescriptor = new DirectorySecurity();
                    newDescriptor.SetSecurityDescriptorBinaryForm(securityDescriptorBinary, sections);
                    new DirectoryInfo(path).SetAccessControl(newDescriptor);
                    WriteSecurityDescriptorObject(newDescriptor, path);
                }
                else
                {
                    FileSecurity newDescriptor = new FileSecurity();
                    newDescriptor.SetSecurityDescriptorBinaryForm(securityDescriptorBinary, sections);
                    new FileInfo(path).SetAccessControl(newDescriptor);
                    WriteSecurityDescriptorObject(newDescriptor, path);
                }
            }
            finally
            {
                PlatformInvokes.RestoreTokenPrivilege("SeRestorePrivilege", ref currentPrivilegeState);
            }
        }
        /// <summary>
        /// Gets the SecurityDescriptor at the specified path, including only the specified
        /// AccessControlSections.
        /// </summary>
        /// <param name="path">
        /// The path of the item to retrieve. It may be a drive or provider-qualified path and may include.
        /// glob characters.
        /// </param>
        /// <param name="sections">
        /// The sections of the security descriptor to include.
        /// </param>
        /// <returns>
        /// Nothing. An object that represents the security descriptor for the item
        /// specified by path is written to the context's pipeline.
        /// </returns>
        /// <exception cref="System.ArgumentException">
        ///     path is null or empty.
        ///     path doesn't exist
        ///     sections is not valid.
        /// </exception>
        public void GetSecurityDescriptor(string path,
                                          AccessControlSections sections)
        {
            ObjectSecurity sd = null;

            path = NormalizePath(path);

            if (string.IsNullOrEmpty(path))
            {
                throw PSTraceSource.NewArgumentNullException("path");
            }

            if ((sections & ~AccessControlSections.All) != 0)
            {
                throw PSTraceSource.NewArgumentException("sections");
            }

            var currentPrivilegeState = new PlatformInvokes.TOKEN_PRIVILEGE();

            try
            {
                PlatformInvokes.EnableTokenPrivilege("SeBackupPrivilege", ref currentPrivilegeState);

                if (Directory.Exists(path))
                {
                    sd = new DirectorySecurity(path, sections);
                }
                else
                {
                    sd = new FileSecurity(path, sections);
                }
            }
            catch (System.Security.SecurityException e)
            {
                WriteError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.PermissionDenied, path));
            }
            finally
            {
                PlatformInvokes.RestoreTokenPrivilege("SeBackupPrivilege", ref currentPrivilegeState);
            }

            WriteSecurityDescriptorObject(sd, path);
        }
Exemple #3
0
        private SSHProcessMediator() : base(true)
        {
#if !UNIX
            var inputHandle = PlatformInvokes.GetStdHandle((uint)PlatformInvokes.StandardHandleId.Input);
            originalStdIn = new StreamReader(
                new FileStream(new SafeFileHandle(inputHandle, false), FileAccess.Read));

            var outputHandle = PlatformInvokes.GetStdHandle((uint)PlatformInvokes.StandardHandleId.Output);
            originalStdOut = new OutOfProcessTextWriter(
                new StreamWriter(
                    new FileStream(new SafeFileHandle(outputHandle, false), FileAccess.Write)));

            var errorHandle = PlatformInvokes.GetStdHandle((uint)PlatformInvokes.StandardHandleId.Error);
            originalStdErr = new OutOfProcessTextWriter(
                new StreamWriter(
                    new FileStream(new SafeFileHandle(errorHandle, false), FileAccess.Write)));
#else
            originalStdIn  = new StreamReader(Console.OpenStandardInput(), true);
            originalStdOut = new OutOfProcessTextWriter(
                new StreamWriter(Console.OpenStandardOutput()));
            originalStdErr = new OutOfProcessTextWriter(
                new StreamWriter(Console.OpenStandardError()));
#endif
        }
        // Handles FDI notification
        internal static IntPtr FdiNotify(FdiNotificationType fdint, FdiNotification fdin)
        {
            switch (fdint)
            {
            case FdiNotificationType.FdintCOPY_FILE:
            {
                // TODO: Should I catch exceptions for the new functions?

                // Copy target directory
                string destPath = Marshal.PtrToStringAnsi(fdin.pv);

                // Split the path to a filename and path
                string fileName          = Path.GetFileName(fdin.psz1);
                string remainingPsz1Path = Path.GetDirectoryName(fdin.psz1);
                destPath = Path.Combine(destPath, remainingPsz1Path);

                Directory.CreateDirectory(destPath);         // Creates all intermediate directories if necessary.

                // Create the file
                string absoluteFilePath = Path.Combine(destPath, fileName);
                return(CabinetNativeApi.FdiOpen(absoluteFilePath, (int)OpFlags.Create, (int)(PermissionMode.Read | PermissionMode.Write)));        // TODO: OK to ignore _O_SEQUENTIAL, WrOnly, and _O_BINARY?
            }

            case FdiNotificationType.FdintCLOSE_FILE_INFO:
            {
                // Close the file
                CabinetNativeApi.FdiClose(fdin.hf);

                // Set the file attributes
                string destPath         = Marshal.PtrToStringAnsi(fdin.pv);
                string absoluteFilePath = Path.Combine(destPath, fdin.psz1);

                IntPtr hFile = PlatformInvokes.CreateFile(
                    absoluteFilePath,
                    PlatformInvokes.FileDesiredAccess.GenericRead | PlatformInvokes.FileDesiredAccess.GenericWrite,
                    PlatformInvokes.FileShareMode.Read,
                    IntPtr.Zero,
                    PlatformInvokes.FileCreationDisposition.OpenExisting,
                    PlatformInvokes.FileAttributes.Normal,
                    IntPtr.Zero);

                if (hFile != IntPtr.Zero)
                {
                    PlatformInvokes.FILETIME ftFile = new PlatformInvokes.FILETIME();
                    if (PlatformInvokes.DosDateTimeToFileTime(fdin.date, fdin.time, ftFile))
                    {
                        PlatformInvokes.FILETIME ftLocal = new PlatformInvokes.FILETIME();
                        if (PlatformInvokes.LocalFileTimeToFileTime(ftFile, ftLocal))
                        {
                            PlatformInvokes.SetFileTime(hFile, ftLocal, null, ftLocal);
                        }
                    }

                    PlatformInvokes.CloseHandle(hFile);
                }

                PlatformInvokes.SetFileAttributesW(
                    absoluteFilePath,
                    (PlatformInvokes.FileAttributes)fdin.attribs & (PlatformInvokes.FileAttributes.ReadOnly | PlatformInvokes.FileAttributes.Hidden | PlatformInvokes.FileAttributes.System | PlatformInvokes.FileAttributes.Archive));

                // Call notification function
                return(new IntPtr(1));
            }
            }

            return(new IntPtr(0));
        }