Esempio n. 1
0
        public VbeSettings(IVBE vbe, IRegistryWrapper registry)
        {
            try
            {
                Version = VbeDllVersion.GetCurrentVersion(vbe);
                switch (Version)
                {
                case DllVersion.Vbe7:
                case DllVersion.Vbe6:
                    _activeRegistryRootPath = string.Format(VbeSettingPathTemplate, vbe.Version.Substring(0, 3));
                    break;

                case DllVersion.Vb98:
                    _activeRegistryRootPath = Vb6SettingPath;
                    break;

                default:
                    Version = DllVersion.Unknown;
                    break;
                }
            }
            catch
            {
                Version = DllVersion.Unknown;
                _activeRegistryRootPath = null;
            }
            _registry = registry;
        }
Esempio n. 2
0
        public VBESettings(IVBE vbe, IRegistryWrapper registry)
        {
            try
            {
                switch (VBEDllVersion.GetCurrentVersion(vbe))
                {
                case DllVersion.Vbe6:
                    Version = DllVersion.Vbe6;
                    _activeRegistryRootPath = Vbe6SettingPath;
                    break;

                case DllVersion.Vbe7:
                    Version = DllVersion.Vbe7;
                    _activeRegistryRootPath = Vbe7SettingPath;
                    break;

                default:
                    Version = DllVersion.Unknown;
                    break;
                }
            }
            catch
            {
                Version = DllVersion.Unknown;
                _activeRegistryRootPath = null;
            }
            _registry = registry;
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the security descriptor for the item specified by <paramref name="path"/>.
        /// </summary>
        ///
        /// <param name="path">
        /// The path to the item.
        /// </param>
        ///
        /// <param name="sections">
        /// Specifies the parts of a security descriptor to retrieve.
        /// </param>
        ///
        /// <returns>
        /// Nothing. An object that represents the security descriptor for the item
        /// specified by path is written to the WriteSecurityDescriptorObject method.
        /// </returns>
        public void GetSecurityDescriptor(string path,
                                          AccessControlSections sections)
        {
            ObjectSecurity   sd  = null;
            IRegistryWrapper key = null;

            // Validate input first.
            if (String.IsNullOrEmpty(path))
            {
                throw PSTraceSource.NewArgumentNullException("path");
            }

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

            path = NormalizePath(path);

            key = GetRegkeyForPathWriteIfError(path, false);

            if (key != null)
            {
                try
                {
                    sd = key.GetAccessControl(sections);
                }
                catch (System.Security.SecurityException e)
                {
                    WriteError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.PermissionDenied, path));
                    return;
                }
                WriteSecurityDescriptorObject(sd, path);
            }
        }
        public void Setup()
        {
            _registryWrapper = MockRepository.GenerateMock <IRegistryWrapper>();
            _registryKey     = MockRepository.GenerateStub <IRegistryKeyWrapper>();

            _subject     = new RegistryKeyValueFinder(_registryWrapper);
            _keysToCheck = new[] { @"SOFTWARE\App1\SubKey\InstallPath1", @"SOFTWARE\App2\InstallPath2" };

            _registryWrapper.Stub(x => x.OpenLocalMachineKey("SOFTWARE")).Return(_registryKey);
        }
        public void Setup()
        {
            _registryWrapper = MockRepository.GenerateMock<IRegistryWrapper>();
            _registryKey = MockRepository.GenerateStub<IRegistryKeyWrapper>();

            _subject = new RegistryKeyValueFinder(_registryWrapper);
            _keysToCheck = new[] { @"SOFTWARE\App1\SubKey\InstallPath1", @"SOFTWARE\App2\InstallPath2" };

            _registryWrapper.Stub(x => x.OpenLocalMachineKey("SOFTWARE")).Return(_registryKey);
        }
Esempio n. 6
0
 private void WriteRegistryItemObject(IRegistryWrapper key, string path)
 {
     if (key != null)
     {
         path = path.Replace('/', '\\');
         path = EscapeSpecialChars(path);
         PSObject item = PSObject.AsPSObject(key.RegistryKey);
         string[] valueNames = key.GetValueNames();
         for (int i = 0; i < valueNames.Length; i++)
         {
             if (string.IsNullOrEmpty(valueNames[i]))
             {
                 valueNames[i] = this.GetLocalizedDefaultToken();
                 break;
             }
         }
         item.AddOrSetProperty("Property", valueNames);
         base.WriteItemObject(item, path, true);
     }
 }
Esempio n. 7
0
 private void SetRegistryValue(IRegistryWrapper key, string propertyName, object value, RegistryValueKind kind, string path, bool writeResult)
 {
     string valueName = this.GetPropertyName(propertyName);
     RegistryValueKind unknown = RegistryValueKind.Unknown;
     if (kind == RegistryValueKind.Unknown)
     {
         unknown = GetValueKindForProperty(key, valueName);
     }
     if (unknown != RegistryValueKind.Unknown)
     {
         try
         {
             value = ConvertValueToKind(value, unknown);
             kind = unknown;
         }
         catch (InvalidCastException)
         {
             unknown = RegistryValueKind.Unknown;
         }
     }
     if (unknown == RegistryValueKind.Unknown)
     {
         if (kind == RegistryValueKind.Unknown)
         {
             if (value != null)
             {
                 kind = GetValueKindFromObject(value);
             }
             else
             {
                 kind = RegistryValueKind.String;
             }
         }
         value = ConvertValueToKind(value, kind);
     }
     key.SetValue(valueName, value, kind);
     if (writeResult)
     {
         object obj2 = key.GetValue(valueName);
         this.WriteWrappedPropertyObject(obj2, propertyName, path);
     }
 }
Esempio n. 8
0
 private void SetRegistryValue(IRegistryWrapper key, string propertyName, object value, RegistryValueKind kind, string path)
 {
     this.SetRegistryValue(key, propertyName, value, kind, path, true);
 }
Esempio n. 9
0
        private void CopyProperty(
            IRegistryWrapper sourceKey,
            IRegistryWrapper destinationKey,
            string sourceProperty,
            string destinationProperty,
            bool writeOnSuccess)
        {
            string realSourceProperty = GetPropertyName(sourceProperty);
            string realDestinationProperty = GetPropertyName(destinationProperty);

            object sourceValue = sourceKey.GetValue(sourceProperty);
            RegistryValueKind sourceKind = sourceKey.GetValueKind(sourceProperty);

            destinationKey.SetValue(destinationProperty, sourceValue, sourceKind);

            if (writeOnSuccess)
            {
                WriteWrappedPropertyObject(sourceValue, realSourceProperty, sourceKey.Name);
            }
        } // CopyProperty
Esempio n. 10
0
        private void GetFilteredRegistryKeyProperties(string path,
                                                                    Collection<string> propertyNames,
                                                                    bool getAll,
                                                                    bool writeAccess,
                                                                    out IRegistryWrapper key,
                                                                    out Collection<string> filteredCollection)
        {
            bool expandAll = false;

            if (String.IsNullOrEmpty(path))
            {
                throw PSTraceSource.NewArgumentException("path");
            }


            filteredCollection = new Collection<string>();
            key = GetRegkeyForPathWriteIfError(path, writeAccess);

            if (key == null)
            {
                return;
            }

            // If properties were not specified, get all the values

            if (propertyNames == null)
            {
                propertyNames = new Collection<string>();
            }

            if (propertyNames.Count == 0 && getAll)
            {
                propertyNames.Add("*");
                expandAll = true;
            }

            string[] valueNames;
            try
            {
                valueNames = key.GetValueNames();
            }
            catch (System.IO.IOException ioException)
            {
                // An exception occurred while trying to get the key. Write
                // out the error.

                WriteError(new ErrorRecord(ioException, ioException.GetType().FullName, ErrorCategory.ReadError, path));
                return;
            }
            catch (System.Security.SecurityException securityException)
            {
                // An exception occurred while trying to get the key. Write
                // out the error.

                WriteError(new ErrorRecord(securityException, securityException.GetType().FullName, ErrorCategory.PermissionDenied, path));
                return;
            }
            catch (System.UnauthorizedAccessException unauthorizedAccessException)
            {
                // An exception occurred while trying to get the key. Write
                // out the error.

                WriteError(new ErrorRecord(unauthorizedAccessException, unauthorizedAccessException.GetType().FullName, ErrorCategory.PermissionDenied, path));
                return;
            }

            foreach (string requestedValueName in propertyNames)
            {
                WildcardPattern valueNameMatcher =
                    WildcardPattern.Get(
                        requestedValueName,

                        WildcardOptions.IgnoreCase);

                bool hadAMatch = false;

                foreach (string valueName in valueNames)
                {
                    string valueNameToMatch = valueName;

                    // Need to convert the default value name to "(default)"
                    if (String.IsNullOrEmpty(valueName))
                    {
                        // Only do the conversion if the caller isn't asking for
                        // "" or null.

                        if (!String.IsNullOrEmpty(requestedValueName))
                        {
                            valueNameToMatch = GetLocalizedDefaultToken();
                        }
                    }

                    if (
                        expandAll ||
                        ((Context.SuppressWildcardExpansion == false) && (valueNameMatcher.IsMatch(valueNameToMatch))) ||
                       ((Context.SuppressWildcardExpansion == true) && (String.Equals(valueNameToMatch, requestedValueName, StringComparison.OrdinalIgnoreCase))))
                    {
                        if (String.IsNullOrEmpty(valueNameToMatch))
                        {
                            // If the value name is empty then using "(default)"
                            // as the property name when adding the note, as
                            // PSObject does not allow an empty propertyName

                            valueNameToMatch = GetLocalizedDefaultToken();
                        }
                        hadAMatch = true;
                        filteredCollection.Add(valueName);
                    }
                } // foreach

                WriteErrorIfPerfectMatchNotFound(hadAMatch, path, requestedValueName);
            } // foreach
        }
Esempio n. 11
0
 private void GetFilteredRegistryKeyProperties(string path, Collection<string> propertyNames, bool getAll, bool writeAccess, out IRegistryWrapper key, out Collection<string> filteredCollection)
 {
     bool flag = false;
     if (string.IsNullOrEmpty(path))
     {
         throw PSTraceSource.NewArgumentException("path");
     }
     filteredCollection = new Collection<string>();
     key = this.GetRegkeyForPathWriteIfError(path, writeAccess);
     if (key != null)
     {
         if (propertyNames == null)
         {
             propertyNames = new Collection<string>();
         }
         if ((propertyNames.Count == 0) && getAll)
         {
             propertyNames.Add("*");
             flag = true;
         }
         string[] valueNames = new string[0];
         try
         {
             valueNames = key.GetValueNames();
         }
         catch (IOException exception)
         {
             base.WriteError(new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.ReadError, path));
             return;
         }
         catch (SecurityException exception2)
         {
             base.WriteError(new ErrorRecord(exception2, exception2.GetType().FullName, ErrorCategory.PermissionDenied, path));
             return;
         }
         catch (UnauthorizedAccessException exception3)
         {
             base.WriteError(new ErrorRecord(exception3, exception3.GetType().FullName, ErrorCategory.PermissionDenied, path));
             return;
         }
         foreach (string str in propertyNames)
         {
             WildcardPattern pattern = new WildcardPattern(str, WildcardOptions.IgnoreCase);
             bool hadAMatch = false;
             foreach (string str2 in valueNames)
             {
                 string input = str2;
                 if (string.IsNullOrEmpty(str2) && !string.IsNullOrEmpty(str))
                 {
                     input = this.GetLocalizedDefaultToken();
                 }
                 if ((flag || (!base.Context.SuppressWildcardExpansion && pattern.IsMatch(input))) || (base.Context.SuppressWildcardExpansion && string.Equals(input, str, StringComparison.OrdinalIgnoreCase)))
                 {
                     if (string.IsNullOrEmpty(input))
                     {
                         input = this.GetLocalizedDefaultToken();
                     }
                     hadAMatch = true;
                     filteredCollection.Add(str2);
                 }
             }
             this.WriteErrorIfPerfectMatchNotFound(hadAMatch, path, str);
         }
     }
 }
 internal RegistryKeyValueFinder(IRegistryWrapper registryWrapper)
 {
     _registryWrapper = registryWrapper;
 }
Esempio n. 13
0
        /// <summary>
        /// Wraps a registry item in a PSObject and sets the TreatAs to 
        /// Microsoft.Win32.RegistryKey. This way values will be presented
        /// in the same format as keys
        /// </summary>
        /// 
        /// <param name="key">
        /// The registry key to be written out.
        /// </param>
        /// 
        /// <param name="path">
        /// The path to the item being written out.
        /// </param>
        /// 
        private void WriteRegistryItemObject(
            IRegistryWrapper key,
            string path)
        {
            if (key == null)
            {
                Dbg.Diagnostics.Assert(
                    key != null,
                    "The RegistryProvider should never attempt to write out a null value");

                // Don't error, but don't write out anything either.
                return;
            }

            // Escape any wildcard characters in the path
            path = EscapeSpecialChars(path);

            // Wrap the key in an PSObject
            PSObject outputObject = PSObject.AsPSObject(key.RegistryKey);

            // Add the registry values to the PSObject
            string[] valueNames = key.GetValueNames();

            for (int index = 0; index < valueNames.Length; ++index)
            {
                if (String.IsNullOrEmpty(valueNames[index]))
                {
                    // The first unnamed value becomes the default value
                    valueNames[index] = GetLocalizedDefaultToken();
                    break;
                }
            }

            outputObject.AddOrSetProperty("Property", valueNames);

            WriteItemObject(outputObject, path, true);
        } // WriteRegistryItemObject
Esempio n. 14
0
        /// <summary>
        /// helper to read back an existing registry key value
        /// </summary>
        /// <param name="key">key to read the value from</param>
        /// <param name="valueName">name of the value to read</param>
        /// <returns>value of the key, null if it could not retrieve
        /// it because known exceptions were thrown, else an exception is percolated up
        /// </returns>
        private static object ReadExistingKeyValue(IRegistryWrapper key, string valueName)
        {
            try
            {
                // Since SetValue can munge the data to a specified 
                // type (RegistryValueKind), retrieve the value again
                // to output it in the correct form to the user.

                return key.GetValue(valueName, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
            }
            catch (System.IO.IOException)
            {
            }
            catch (System.Security.SecurityException)
            {
            }
            catch (System.UnauthorizedAccessException)
            {
            }
            return null;
        }
Esempio n. 15
0
        /// <summary>
        /// Helper to get RegistryValueKind for a Property
        /// </summary>
        /// <param name="key">RegistryKey containing property</param>
        /// <param name="valueName">Property for which RegistryValueKind is requested</param>
        /// <returns>RegistryValueKind of the property. If the property does not exit,returns RegistryValueKind.Unknown</returns>
        private static RegistryValueKind GetValueKindForProperty(IRegistryWrapper key, string valueName)
        {
            try
            {
                return key.GetValueKind(valueName);
            }
            catch (System.ArgumentException)
            {
                //RegistryKey that contains the specified value does not exist
            }
            catch (System.IO.IOException)
            {
            }
            catch (System.Security.SecurityException)
            {
            }
            catch (System.UnauthorizedAccessException)
            {
            }

            return RegistryValueKind.Unknown;
        }
Esempio n. 16
0
        /// <summary>
        /// Sets or creates a registry value on a key.
        /// </summary>
        /// 
        /// <param name="key">
        /// The key to set or create the value on.
        /// </param>
        /// 
        /// <param name="propertyName">
        /// The name of the value to set or create.
        /// </param>
        /// 
        /// <param name="value">
        /// The new data for the value.
        /// </param>
        /// 
        /// <param name="kind">
        /// The RegistryValueKind of the value.
        /// </param>
        /// 
        /// <param name="path">
        /// The path to the key that the value is being set on.
        /// </param>
        /// 
        /// <param name="writeResult">
        /// If true, the value that is set will be written out.
        /// </param>
        /// 
        private void SetRegistryValue(
            IRegistryWrapper key,
            string propertyName,
            object value,
            RegistryValueKind kind,
            string path,
            bool writeResult)
        {
            Dbg.Diagnostics.Assert(
                key != null,
                "Caller should have verified key");

            string propertyNameToSet = GetPropertyName(propertyName);

            RegistryValueKind existingKind = RegistryValueKind.Unknown;

            // If user does not specify a kind: get the valuekind if the property
            // already exists
            if (kind == RegistryValueKind.Unknown)
            {
                existingKind = GetValueKindForProperty(key, propertyNameToSet);
            }

            // try to do a conversion based on the existing kind, if we
            // were able to retrieve one
            if (existingKind != RegistryValueKind.Unknown)
            {
                try
                {
                    value = ConvertValueToKind(value, existingKind);
                    kind = existingKind;
                }
                catch (InvalidCastException)
                {
                    // failed attempt, we reset to unknown to let the
                    // default conversion process take over
                    existingKind = RegistryValueKind.Unknown;
                }
            }

            // set the kind as defined by the user
            if (existingKind == RegistryValueKind.Unknown)
            {
                // we use to kind passed in, either because we had
                // a valid one or because we failed to retrieve an existing kind to match
                if (kind == RegistryValueKind.Unknown)
                {
                    // set the kind based on value
                    if (value != null)
                    {
                        kind = GetValueKindFromObject(value);
                    }
                    else
                    {
                        // if no value and unknown kind, then default to empty string
                        kind = RegistryValueKind.String;
                    }
                }

                value = ConvertValueToKind(value, kind);
            }

            key.SetValue(propertyNameToSet, value, kind);

            if (writeResult)
            {
                // Now write out the value
                object newValue = key.GetValue(propertyNameToSet);

                WriteWrappedPropertyObject(newValue, propertyName, path);
            }
        } // SetRegistryValue
Esempio n. 17
0
        /// <summary>
        /// IT resets the a registry key value to its default
        /// </summary>
        /// <param name="key">Key whose value has to be reset</param>
        /// <param name="valueName">name of the value to reset</param>
        /// <returns>default value the key was set to</returns>
        private object ResetRegistryKeyValue(IRegistryWrapper key, string valueName)
        {
            RegistryValueKind valueKind = key.GetValueKind(valueName);
            object defaultValue = null;

            switch (valueKind)
            {
                // NOTICE: we assume that an unknown type is treated as
                // the same as a binary blob
                case RegistryValueKind.Binary:
                case RegistryValueKind.Unknown:
                    {
                        defaultValue = new byte[0];
                    }
                    break;
                case RegistryValueKind.DWord:
                    {
                        defaultValue = (int)0;
                    }
                    break;
                case RegistryValueKind.ExpandString:
                case RegistryValueKind.String:
                    {
                        defaultValue = "";
                    }
                    break;
                case RegistryValueKind.MultiString:
                    {
                        defaultValue = new string[0];
                    }
                    break;
                case RegistryValueKind.QWord:
                    {
                        defaultValue = (long)0;
                    }
                    break;
            }

            try
            {
                key.SetValue(valueName, defaultValue, valueKind);
            }
            catch (System.IO.IOException ioException)
            {
                // An exception occurred while trying to set the value. Write
                // out the error.

                WriteError(new ErrorRecord(ioException, ioException.GetType().FullName, ErrorCategory.WriteError, valueName));
            }
            catch (System.Security.SecurityException securityException)
            {
                // An exception occurred while trying to set the value. Write
                // out the error.

                WriteError(new ErrorRecord(securityException, securityException.GetType().FullName, ErrorCategory.PermissionDenied, valueName));
            }
            catch (System.UnauthorizedAccessException unauthorizedAccessException)
            {
                // An exception occurred while trying to get the key. Write
                // out the error.

                WriteError(new ErrorRecord(unauthorizedAccessException, unauthorizedAccessException.GetType().FullName, ErrorCategory.PermissionDenied, valueName));
            }
            return defaultValue;
        }
Esempio n. 18
0
 private void CopyProperty(IRegistryWrapper sourceKey, IRegistryWrapper destinationKey, string sourceProperty, string destinationProperty, bool writeOnSuccess)
 {
     string propertyName = this.GetPropertyName(sourceProperty);
     this.GetPropertyName(destinationProperty);
     object obj2 = sourceKey.GetValue(sourceProperty);
     RegistryValueKind valueKind = sourceKey.GetValueKind(sourceProperty);
     destinationKey.SetValue(destinationProperty, obj2, valueKind);
     if (writeOnSuccess)
     {
         this.WriteWrappedPropertyObject(obj2, propertyName, sourceKey.Name);
     }
 }
Esempio n. 19
0
 private bool CopyRegistryKey(IRegistryWrapper key, string path, string destination, bool recurse, bool streamResult, bool streamFirstOnly)
 {
     bool flag = true;
     if (recurse && this.ErrorIfDestinationIsSourceOrChildOfSource(path, destination))
     {
         return false;
     }
     tracer.WriteLine("destination = {0}", new object[] { destination });
     IRegistryWrapper regkeyForPath = this.GetRegkeyForPath(destination, true);
     string childName = this.GetChildName(path);
     string parentPath = destination;
     if (regkeyForPath == null)
     {
         parentPath = this.GetParentPath(destination, null);
         childName = this.GetChildName(destination);
         regkeyForPath = this.GetRegkeyForPathWriteIfError(parentPath, true);
     }
     if (regkeyForPath == null)
     {
         return false;
     }
     string str3 = this.MakePath(parentPath, childName);
     string copyKeyAction = RegistryProviderStrings.CopyKeyAction;
     string copyKeyResourceTemplate = RegistryProviderStrings.CopyKeyResourceTemplate;
     string target = string.Format(base.Host.CurrentCulture, copyKeyResourceTemplate, new object[] { path, destination });
     if (base.ShouldProcess(target, copyKeyAction))
     {
         IRegistryWrapper wrapper2 = null;
         try
         {
             wrapper2 = regkeyForPath.CreateSubKey(childName);
         }
         catch (NotSupportedException exception)
         {
             base.WriteError(new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.InvalidOperation, childName));
         }
         if (wrapper2 != null)
         {
             string[] valueNames = key.GetValueNames();
             for (int i = 0; i < valueNames.Length; i++)
             {
                 if (base.Stopping)
                 {
                     regkeyForPath.Close();
                     wrapper2.Close();
                     return false;
                 }
                 wrapper2.SetValue(valueNames[i], key.GetValue(valueNames[i], null, RegistryValueOptions.DoNotExpandEnvironmentNames), key.GetValueKind(valueNames[i]));
             }
             if (streamResult)
             {
                 this.WriteRegistryItemObject(wrapper2, str3);
                 if (streamFirstOnly)
                 {
                     streamResult = false;
                 }
             }
         }
     }
     regkeyForPath.Close();
     if (recurse)
     {
         string[] subKeyNames = key.GetSubKeyNames();
         for (int j = 0; j < subKeyNames.Length; j++)
         {
             if (base.Stopping)
             {
                 return false;
             }
             string str7 = this.MakePath(path, subKeyNames[j]);
             string str8 = this.MakePath(str3, subKeyNames[j]);
             IRegistryWrapper wrapper3 = this.GetRegkeyForPath(str7, false);
             bool flag2 = this.CopyRegistryKey(wrapper3, str7, str8, recurse, streamResult, streamFirstOnly);
             wrapper3.Close();
             if (!flag2)
             {
                 flag = flag2;
             }
         }
     }
     return flag;
 }
Esempio n. 20
0
        } // CopyProperty

        private void MoveProperty(
            IRegistryWrapper sourceKey,
            IRegistryWrapper destinationKey,
            string sourceProperty,
            string destinationProperty)
        {
            string realSourceProperty = GetPropertyName(sourceProperty);
            string realDestinationProperty = GetPropertyName(destinationProperty);

            try
            {
                // If sourceProperty and destinationProperty happens to be the same
                // then we shouldn't remove the property
                bool continueWithRemove = true;

                if (string.Equals(sourceKey.Name, destinationKey.Name, StringComparison.OrdinalIgnoreCase) &&
                    string.Equals(realSourceProperty, realDestinationProperty, StringComparison.OrdinalIgnoreCase))
                {
                    continueWithRemove = false;
                }

                // Move is implemented by copying the value and then deleting the original
                // Copy property will throw an exception if it fails

                CopyProperty(
                    sourceKey,
                    destinationKey,
                    realSourceProperty,
                    realDestinationProperty,
                    false);

                // Delete sourceproperty only if it is not same as destination property
                if (continueWithRemove)
                {
                    sourceKey.DeleteValue(realSourceProperty);
                }

                object newValue = destinationKey.GetValue(realDestinationProperty);
                WriteWrappedPropertyObject(newValue, destinationProperty, destinationKey.Name);
            }
            catch (System.IO.IOException ioException)
            {
                // An exception occurred while trying to get the key. Write
                // out the error.

                WriteError(new ErrorRecord(ioException, ioException.GetType().FullName, ErrorCategory.WriteError, sourceKey.Name));
                return;
            }
            catch (System.Security.SecurityException securityException)
            {
                // An exception occurred while trying to get the key. Write
                // out the error.

                WriteError(new ErrorRecord(securityException, securityException.GetType().FullName, ErrorCategory.PermissionDenied, sourceKey.Name));
                return;
            }
            catch (System.UnauthorizedAccessException unauthorizedAccessException)
            {
                // An exception occurred while trying to get the key. Write
                // out the error.

                WriteError(new ErrorRecord(unauthorizedAccessException, unauthorizedAccessException.GetType().FullName, ErrorCategory.PermissionDenied, sourceKey.Name));
                return;
            }
        } // MoveProperty
Esempio n. 21
0
 private static RegistryValueKind GetValueKindForProperty(IRegistryWrapper key, string valueName)
 {
     try
     {
         return key.GetValueKind(valueName);
     }
     catch (ArgumentException)
     {
     }
     catch (IOException)
     {
     }
     catch (SecurityException)
     {
     }
     catch (UnauthorizedAccessException)
     {
     }
     return RegistryValueKind.Unknown;
 }
Esempio n. 22
0
 private void MoveProperty(IRegistryWrapper sourceKey, IRegistryWrapper destinationKey, string sourceProperty, string destinationProperty)
 {
     string propertyName = this.GetPropertyName(sourceProperty);
     string b = this.GetPropertyName(destinationProperty);
     try
     {
         bool flag = true;
         if (string.Equals(sourceKey.Name, destinationKey.Name, StringComparison.OrdinalIgnoreCase) && string.Equals(propertyName, b, StringComparison.OrdinalIgnoreCase))
         {
             flag = false;
         }
         this.CopyProperty(sourceKey, destinationKey, propertyName, b, false);
         if (flag)
         {
             sourceKey.DeleteValue(propertyName);
         }
         object obj2 = destinationKey.GetValue(b);
         this.WriteWrappedPropertyObject(obj2, destinationProperty, destinationKey.Name);
     }
     catch (IOException exception)
     {
         base.WriteError(new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.WriteError, sourceKey.Name));
     }
     catch (SecurityException exception2)
     {
         base.WriteError(new ErrorRecord(exception2, exception2.GetType().FullName, ErrorCategory.PermissionDenied, sourceKey.Name));
     }
     catch (UnauthorizedAccessException exception3)
     {
         base.WriteError(new ErrorRecord(exception3, exception3.GetType().FullName, ErrorCategory.PermissionDenied, sourceKey.Name));
     }
 }
 internal RegistryKeyValueFinder(IRegistryWrapper registryWrapper)
 {
     _registryWrapper = registryWrapper;
 }
Esempio n. 24
0
 private static object ReadExistingKeyValue(IRegistryWrapper key, string valueName)
 {
     try
     {
         return key.GetValue(valueName, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
     }
     catch (IOException)
     {
     }
     catch (SecurityException)
     {
     }
     catch (UnauthorizedAccessException)
     {
     }
     return null;
 }
Esempio n. 25
0
 public IndexerDriveDetection(IRegistryWrapper registryHelper)
 {
     _registryHelper = registryHelper;
     GetEnhancedModeStatus();
 }
Esempio n. 26
0
        private object ResetRegistryKeyValue(IRegistryWrapper key, string valueName)
        {
            RegistryValueKind valueKind = key.GetValueKind(valueName);
            object obj2 = null;
            switch (valueKind)
            {
                case RegistryValueKind.Unknown:
                case RegistryValueKind.Binary:
                    obj2 = new byte[0];
                    break;

                case RegistryValueKind.String:
                case RegistryValueKind.ExpandString:
                    obj2 = "";
                    break;

                case RegistryValueKind.DWord:
                    obj2 = 0;
                    break;

                case RegistryValueKind.MultiString:
                    obj2 = new string[0];
                    break;

                case RegistryValueKind.QWord:
                    obj2 = 0L;
                    break;
            }
            try
            {
                key.SetValue(valueName, obj2, valueKind);
            }
            catch (IOException exception)
            {
                base.WriteError(new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.WriteError, valueName));
            }
            catch (SecurityException exception2)
            {
                base.WriteError(new ErrorRecord(exception2, exception2.GetType().FullName, ErrorCategory.PermissionDenied, valueName));
            }
            catch (UnauthorizedAccessException exception3)
            {
                base.WriteError(new ErrorRecord(exception3, exception3.GetType().FullName, ErrorCategory.PermissionDenied, valueName));
            }
            return obj2;
        }
Esempio n. 27
0
        /// <summary>
        /// Sets the security descriptor for the item specified by <paramref name="path"/>
        /// </summary>
        ///
        /// <param name="path">
        /// The path to the item to set the security descriptor on.
        /// </param>
        ///
        /// <param name="securityDescriptor">
        /// The new security descriptor for the item.
        /// </param>
        public void SetSecurityDescriptor(
            string path,
            ObjectSecurity securityDescriptor)
        {
            IRegistryWrapper key = null;

            if (String.IsNullOrEmpty(path))
            {
                throw PSTraceSource.NewArgumentException("path");
            }

            if (securityDescriptor == null)
            {
                throw PSTraceSource.NewArgumentNullException("securityDescriptor");
            }

            path = NormalizePath(path);

            ObjectSecurity sd;

            if (TransactionAvailable())
            {
                sd = securityDescriptor as TransactedRegistrySecurity;

                if (sd == null)
                {
                    throw PSTraceSource.NewArgumentException("securityDescriptor");
                }
            }
            else
            {
                sd = securityDescriptor as RegistrySecurity;

                if (sd == null)
                {
                    throw PSTraceSource.NewArgumentException("securityDescriptor");
                }
            }

            key = GetRegkeyForPathWriteIfError(path, true);

            if (key != null)
            {
                //
                // the caller already checks for the following exceptions:
                // -- UnauthorizedAccessException
                // -- PrivilegeNotHeldException
                // -- NotSupportedException
                // -- SystemException
                //
                try
                {
                    key.SetAccessControl(sd);
                }
                catch (System.Security.SecurityException e)
                {
                    WriteError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.PermissionDenied, path));
                    return;
                }
                catch (System.UnauthorizedAccessException e)
                {
                    WriteError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.PermissionDenied, path));
                    return;
                }

                WriteSecurityDescriptorObject(sd, path);
            }
        } // SetSecurityDescriptor
Esempio n. 28
0
        } // CopyItem


        private bool CopyRegistryKey(
            IRegistryWrapper key,
            string path,
            string destination,
            bool recurse,
            bool streamResult,
            bool streamFirstOnly)
        {
            bool result = true;

            // Make sure we are not trying to do a recursive copy of a key
            // to itself or a child of itself.

            if (recurse)
            {
                if (ErrorIfDestinationIsSourceOrChildOfSource(path, destination))
                {
                    return false;
                }
            }

            Dbg.Diagnostics.Assert(
                key != null,
                "The key should have been validated by the caller");

            Dbg.Diagnostics.Assert(
                !String.IsNullOrEmpty(path),
                "The path should have been validated by the caller");

            Dbg.Diagnostics.Assert(
                !String.IsNullOrEmpty(destination),
                "The destination should have been validated by the caller");

            s_tracer.WriteLine("destination = {0}", destination);

            // Get the parent key of the destination
            // If the destination already exists and is a key, then it becomes
            // the container of the source. If the key doesn't already exist
            // the parent of the destination path becomes the container of source.

            IRegistryWrapper newParentKey = GetRegkeyForPath(destination, true);
            string destinationName = GetChildName(path);
            string destinationParent = destination;

            if (newParentKey == null)
            {
                destinationParent = GetParentPath(destination, null);
                destinationName = GetChildName(destination);

                newParentKey = GetRegkeyForPathWriteIfError(destinationParent, true);
            }

            if (newParentKey == null)
            {
                // The key was not found.
                // An error should have been written by GetRegkeyForPathWriteIfError
                return false;
            }

            string destinationPath = MakePath(destinationParent, destinationName);


            // Confirm the copy item with the user

            string action = RegistryProviderStrings.CopyKeyAction;

            string resourceTemplate = RegistryProviderStrings.CopyKeyResourceTemplate;

            string resource =
                    String.Format(
                        Host.CurrentCulture,
                        resourceTemplate,
                        path,
                        destination);

            if (ShouldProcess(resource, action))
            {
                // Create new key under the parent

                IRegistryWrapper newKey = null;
                try
                {
                    newKey = newParentKey.CreateSubKey(destinationName);
                }
                catch (NotSupportedException e)
                {
                    WriteError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.InvalidOperation, destinationName));
                }

                if (newKey != null)
                {
                    // Now copy all the properties from the source to the destination

                    string[] valueNames = key.GetValueNames();

                    for (int index = 0; index < valueNames.Length; ++index)
                    {
                        // Making sure to obey the StopProcessing.
                        if (Stopping)
                        {
                            newParentKey.Close();
                            newKey.Close();
                            return false;
                        }

                        newKey.SetValue(
                            valueNames[index],
                            key.GetValue(valueNames[index], null, RegistryValueOptions.DoNotExpandEnvironmentNames),
                            key.GetValueKind(valueNames[index]));
                    }

                    if (streamResult)
                    {
                        // Write out the key that was copied

                        WriteRegistryItemObject(newKey, destinationPath);

                        if (streamFirstOnly)
                        {
                            streamResult = false;
                        }
                    }
                }
            }

            newParentKey.Close();

            if (recurse)
            {
                // Copy all the subkeys

                string[] subkeyNames = key.GetSubKeyNames();

                for (int keyIndex = 0; keyIndex < subkeyNames.Length; ++keyIndex)
                {
                    // Making sure to obey the StopProcessing.
                    if (Stopping)
                    {
                        return false;
                    }

                    // Make the new path under the copy path.

                    string subKeyPath = MakePath(path, subkeyNames[keyIndex]);
                    string newSubKeyPath = MakePath(destinationPath, subkeyNames[keyIndex]);

                    IRegistryWrapper childKey = GetRegkeyForPath(subKeyPath, false);

                    bool subtreeResult = CopyRegistryKey(childKey, subKeyPath, newSubKeyPath, recurse, streamResult, streamFirstOnly);

                    childKey.Close();

                    if (!subtreeResult)
                    {
                        result = subtreeResult;
                    }
                }
            }

            return result;
        } // CopyRegistryKey