Beispiel #1
0
 public static void ExtractWow64Options(Win32Api.KeySecurity value,
                                        out KeySecurity keySecurity, out Win32Api.RegWow64Options wow64Options)
 {
     wow64Options = (Win32Api.RegWow64Options)((int)value &
                                               (int)(Win32Api.RegWow64Options.KEY_WOW64_32KEY | Win32Api.RegWow64Options.KEY_WOW64_64KEY));
     keySecurity = new KeySecurity(value & (Win32Api.KeySecurity) ~wow64Options);
 }
Beispiel #2
0
 internal void ApplyNotOnlyReadOperation(string subKeyName,
                                         KeySecurity samDesired, KeyImplOperation operation,
                                         Win32Api.RegWow64Options wowOptions = Win32Api.RegWow64Options.None)
 {
     if (!TryApplyOperation(KeyDisposition.DIFF_HIVE, subKeyName, samDesired, operation, wowOptions))
     {
         throw new FileNotFoundException();
     }
 }
Beispiel #3
0
        // precondition: Called only for "default" identities not containing wow6432node
        private string AdjustWow64PathIfNeeded(Win32Api.RegWow64Options wowOptions)
        {
            if (Utils.Is64BitOperatingSystem)
            {
                if (wowOptions == Win32Api.RegWow64Options.None)
                {
                    // Actually for now (May 2013) we can run only in 32bit process, but may be in the future...
                    wowOptions = Utils.Is64BitProcess ? Win32Api.RegWow64Options.KEY_WOW64_64KEY : Win32Api.RegWow64Options.KEY_WOW64_32KEY;
                }

                // Now wowOptions specify definetly, which branch of registry to access
                if (wowOptions == Win32Api.RegWow64Options.KEY_WOW64_32KEY)
                {
                    return(GetWow64RedirectedPath());
                }
            }
            return(systemPathRelativeToBaseKey_);
        }
Beispiel #4
0
        private bool TryGetSubKey(KeyDisposition disposition, string lpSubKey,
                                  KeySecurity samDesired, Win32Api.RegWow64Options wowOptions, out KeyImplHolder subKeyHolder)
        {
            KeyIdentity subKeyIdentity = new KeyIdentity(identity_, wowOptions, lpSubKey);
            IKeyImpl    keyImpl        = cachedKeyImpls_.TryGet(subKeyIdentity, disposition, samDesired);

            if (keyImpl != null)
            {
                subKeyHolder = new NotOwningKeyImplHolder(keyImpl);
                return(true);
            }
            KeyImplHolder newOne;

            if (!opener_.TryOpenHolder(identity_, disposition, subKeyIdentity, samDesired, out newOne))
            {
                subKeyHolder = null;
                return(false);
            }
            subKeyHolder = cachedKeyImpls_.Add(subKeyIdentity, disposition, newOne);
            return(true);
        }
Beispiel #5
0
 internal void Delete(
     string lpSubKey,
     Win32Api.RegWow64Options samDesired,
     int Reserved)
 {
     // We try to delete offreg hive key
     // and we do nothing with the windows key
     // Thus postcondition of this operation is not met, but this is
     // our application logic and concious decision
     ApplyNotOnlyReadOperation(lpSubKey, new KeySecurity(Win32Api.KeySecurity.DELETE),
                               keyImpl =>
     {
         keyImpl.Delete();
         // Cache is cleared here because in cache there may be keyImpls
         // pointing to the deleted key, or its subkeys, which prevent
         // actual deleting of the key until the VirtualKey is closed otherwise.
         // And the marking key as deleted won't work if cache is not cleared for the same reason.
         ReinitializeCache();
         return(true);
     },
                               samDesired);
     // If there is a key in a higher order hive, marking it as deleted
     try
     {
         ApplyReadOperation(lpSubKey, new KeySecurity(Win32Api.KeySecurity.KEY_READ),
                            keyImpl =>
         {
             opener_.ChangeableHive.MarkKeyAsDeleted(
                 new KeyIdentity(identity_, samDesired, lpSubKey));
             return(true);
         });
     }
     catch (Win32Exception)
     {
         // Suppressing all errors like file not found (means no need to mark the
         // key as deleted), or access denied (means deletion can't be performed)
     }
 }
Beispiel #6
0
        // returns false in case of FileNotFound error, otherwise throws exception
        internal bool TryApplyOperation(KeyDisposition disposition, string subKeyName,
                                        KeySecurity samDesired, KeyImplOperation operation,
                                        Win32Api.RegWow64Options wowOptions = Win32Api.RegWow64Options.None)
        {
            // None is passed from Get/Set/Enum operations which does not allow user to pass wow options
            // in such case we take it from the current identity
            if (wowOptions == Win32Api.RegWow64Options.None)
            {
                wowOptions = identity_.GetWow64Mode();
            }
            KeyImplHolder subKey;

            if (!TryGetSubKey(disposition, subKeyName, samDesired, wowOptions, out subKey))
            {
                return(false);
            }
            using (subKey)
            {
                // TODO: almost always the operation needs subKey name, at least for logging
                // so it is constructed second time there.
                // Better to pass it from here.
                try
                {
                    if (!operation(subKey.GetKeyImpl()))
                    {
                        return(false);
                    }
                }
                catch (FileNotFoundException)
                {
                    // TODO: make all operations return false in case of FileNotFoundException
                    // so that this catch can be removed.
                    return(false);
                }
            }
            return(true);
        }
Beispiel #7
0
        internal KeyIdentity(KeyIdentity parentIdentity, Win32Api.RegWow64Options wowOptions, string subKey)
        {
            // Which are the options:
            // 0. The process is either 32 or 64 bit.
            // 1. ParentIdentity is either 32 or 64 or it is too high level key so path does not yet include
            // Wow6432Node, this means "Default" flags.
            // 2. wowOptions is either 32, 64

            // It can be that subKey == null, parentIdentity uses "default" flags,
            // in this case we have to decide add Wow6432Node...
            // and because of so many places where it may appear...
            // better to make some system function do that for us.

            // Resulting wowOptions are assigned wowOptions specified in the function call
            // because as per msdn they MUST be equal to those used in previous calls for opening
            // parent keys.

            // Creating default (32 bit on 32bit OS, 64 bit on 64 bit OS) identity to get canonized path
            KeyIdentity defaultIdentity = new KeyIdentity(parentIdentity.baseKey_, CombinePaths(parentIdentity.GetRegPath(), subKey));

            baseKey_ = defaultIdentity.baseKey_; // Canonized base key
            // Taking care of Wow64 if we are on 64 bit OS after we canonized the path and base key.
            systemPathRelativeToBaseKey_ = defaultIdentity.AdjustWow64PathIfNeeded(wowOptions);
        }