Exemplo n.º 1
0
        protected override void ProcessRecord()
        {
            if (Inherit != Item.NONE || !string.IsNullOrEmpty(Access))
            {
                using (RegistryKey regKey = RegistryControl.GetRegistryKey(Path, true, true))
                {
                    RegistrySecurity security = regKey.GetAccessControl();

                    //  上位からのアクセス権継承の設定変更
                    switch (Inherit)
                    {
                    case Item.ENABLE:
                        TestGenerator.RegistryInherit(Path, true);
                        security.SetAccessRuleProtection(false, false);
                        break;

                    case Item.DISABLE:
                        TestGenerator.RegistryInherit(Path, false);
                        security.SetAccessRuleProtection(true, true);
                        break;

                    case Item.REMOVE:
                        TestGenerator.RegistryInherit(Path, false);
                        security.SetAccessRuleProtection(true, false);
                        break;
                    }

                    //  Access文字列からのアクセス権設定
                    if (!string.IsNullOrEmpty(Access))
                    {
                        foreach (string ruleString in
                                 Access.Contains("/") ? Access.Split('/') : new string[1] {
                            Access
                        })
                        {
                            //  テスト自動生成
                            TestGenerator.RegistryAccess(Path, ruleString, true);

                            security.SetAccessRule(RegistryControl.StringToAccessRule(ruleString));
                        }
                    }

                    /*
                     * foreach (RegistryAccessRule accessRule in RegistryControl.StringToAccess(Access))
                     * {
                     *  security.SetAccessRule(accessRule);
                     * }
                     */
                    regKey.SetAccessControl(security);
                }
            }

            //  所有者変更
            if (Owner != null)
            {
                //  テスト自動生成
                TestGenerator.RegistryOwner(Path, Owner);

                //  埋め込みのsubinacl.exeを展開
                string tempDir = System.IO.Path.Combine(
                    Environment.ExpandEnvironmentVariables("%TEMP%"),
                    "PowerReg");
                string subinacl = System.IO.Path.Combine(tempDir, "subinacl.exe");
                if (!File.Exists(subinacl))
                {
                    EmbeddedResource.Expand(tempDir);
                }

                //  管理者実行確認
                Message.CheckAdmin();

                using (Process proc = new Process())
                {
                    proc.StartInfo.FileName    = subinacl;
                    proc.StartInfo.Arguments   = $"/subkeyreg \"{Path}\" /owner=\"{Owner}\"";
                    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    proc.Start();
                    proc.WaitForExit();
                }
            }

            //  レジストリ値の設定
            if (Name != null)
            {
                //  テスト自動生成
                TestGenerator.RegistryName(Path, Name);
                TestGenerator.RegistryType(Path, Name, Type);
                TestGenerator.RegistryValue(Path, Name, Value);
                switch (Type)
                {
                case Item.REG_SZ:
                    Registry.SetValue(Path, Name, Value, RegistryValueKind.String);
                    break;

                case Item.REG_BINARY:
                    Registry.SetValue(Path, Name, RegistryControl.RegBinaryStringToBytes(Value), RegistryValueKind.Binary);
                    break;

                case Item.REG_DWORD:
                    Registry.SetValue(Path, Name, int.Parse(Value), RegistryValueKind.DWord);
                    break;

                case Item.REG_QWORD:
                    Registry.SetValue(Path, Name, long.Parse(Value), RegistryValueKind.QWord);
                    break;

                case Item.REG_MULTI_SZ:
                    Registry.SetValue(Path, Name, Regex.Split(Value, "\\\\0"), RegistryValueKind.MultiString);
                    break;

                case Item.REG_EXPAND_SZ:
                    Registry.SetValue(Path, Name, Value, RegistryValueKind.ExpandString);
                    break;

                case Item.REG_NONE:
                    Registry.SetValue(Path, Name, new byte[2] {
                        0, 0
                    }, RegistryValueKind.None);
                    break;
                }
            }
        }
Exemplo n.º 2
0
        protected override void ProcessRecord()
        {
            bool isChange = false;

            using (RegistryKey regKey = RegistryControl.GetRegistryKey(Path, false, true))
            {
                if (regKey == null)
                {
                    return;
                }

                RegistrySecurity security = regKey.GetAccessControl();

                //  アクセス権設定
                if (!string.IsNullOrEmpty(Account))
                {
                    RegistryAccessRule rule = new RegistryAccessRule(
                        new NTAccount(Account),
                        (RegistryRights)Enum.Parse(typeof(RegistryRights), _Rights),
                        Recursive ?
                        InheritanceFlags.ContainerInherit :
                        InheritanceFlags.None,
                        PropagationFlags.None,
                        (AccessControlType)Enum.Parse(typeof(AccessControlType), AccessControl));

                    //  テスト自動生成
                    TestGenerator.RegistryAccess(Path, RegistryControl.AccessRuleToString(rule), true);

                    security.SetAccessRule(rule);
                    isChange = true;
                }

                //  Access文字列からの設定
                if (!string.IsNullOrEmpty(Access))
                {
                    /*
                     * foreach (RegistryAccessRule accessRule in RegistryControl.StringToAccess(Access))
                     * {
                     *  security.SetAccessRule(accessRule);
                     *  isChange = true;
                     * }
                     */
                    foreach (string ruleString in
                             Access.Contains("/") ? Access.Split('/') : new string[1] {
                        Access
                    })
                    {
                        //  テスト自動生成
                        TestGenerator.RegistryAccess(Path, ruleString, true);

                        security.SetAccessRule(RegistryControl.StringToAccessRule(ruleString));
                        isChange = true;
                    }
                }

                //  上位からのアクセス権継承の設定変更
                switch (Inherit)
                {
                case Item.ENABLE:
                    TestGenerator.RegistryInherit(Path, true);
                    security.SetAccessRuleProtection(false, false);
                    isChange = true;
                    break;

                case Item.DISABLE:
                    TestGenerator.RegistryInherit(Path, false);
                    security.SetAccessRuleProtection(true, true);
                    isChange = true;
                    break;

                case Item.REMOVE:
                    TestGenerator.RegistryInherit(Path, false);
                    security.SetAccessRuleProtection(true, false);
                    isChange = true;
                    break;
                }

                if (isChange)
                {
                    regKey.SetAccessControl(security);
                }
            }

            WriteObject(new RegistryKeyInfo(Path, true));
        }