<commit_before>﻿//******************************************************************************************************
//  Main.cs - Gbtc
//
//  Copyright © 2013, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
//  not use this file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://www.opensource.org/licenses/MIT
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  12/20/2013 - <NAME>
//       Generated original version of source code.
//
//******************************************************************************************************

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.Win32;

namespace NoInetFixUtil
{
    public partial class Main : Form
    {
        #region [ Members ]

        // Nested Types
        private class Product
        {
            public string Name;
            public string InstallPath;
            public string ServiceOID;
            public int DisableGeneratePublisherEvidence;
        }

        // Constants
        private static readonly string[] ClientOIDs = { "172.16.58.3", "1.3.6.1.5.5.7.1.1" };

        // Fields
        private List<Product> m_products;
        private bool m_checkedEventsEnabled;
        private bool applyAll;
        #endregion

        #region [ Constructors ]

        public Main(bool applyAll)
        {
            InitializeComponent();
            this.applyAll = applyAll;
        }

        #endregion

        #region [ Methods ]

        // Sets the initial state of the checkboxes at startup.
        private void Main_Load(object sender, EventArgs e)
        {
            object disableRootAutoUpdate;

            StatusTextBox.AppendText("Detecting GPA products that are installed on the system... ");

            using (RegistryKey gpaKey = Registry.LocalMachine.OpenSubKey(@"Software\Grid Protection Alliance"))
            {
                if ((object)gpaKey != null)
                {
                    // Populate the list of GPA products installed on the system
                    m_products = gpaKey.GetSubKeyNames()
                        .Select(productName => ConvertToProductAndDispose(gpaKey.OpenSubKey(productName)))
                        .ToList();

                    // Display the list of products that will be affected by the fixes that this tool provides
                    foreach (Product product in m_products)
                    {
                        GPAProductsTextBox.AppendText(Environment.NewLine);
                        GPAProductsTextBox.AppendText(product.Name);
                    }

                    // Determine whether the service OIDs are already registered for any products in the GPA product list
                    if (m_products.Any(product => (object)product.ServiceOID != null))
                    {
                        ServiceOIDCheckBox.Checked = true;

                        // Determine whether any GPA services have been installed or uninstalled since the last time the fix was applied
                        if (m_products.Any(product => (object)product.ServiceOID == null))
                        {
                            AppendStatusMessage("NoInetFixUtil has detected that one or more GPA" +
                                " products have been installed since the service OID fix was last applied." +
                                " To correct this, double-click the \"Register OIDs used by GSF services\" checkbox.");
                        }
                        else if (m_products.Any(product => (object)product.ServiceOID != null && (object)product.InstallPath == null))
                        {
                            AppendStatusMessage("NoInetFixUtil has detected that the service OID fix" +
                                " has been applied to one or more GPA products that have since been uninstalled." +
                                " To correct this, double-click the \"Register OIDs used by GSF services\" checkbox.");
                        }
                    }

                    // Determine whether the publisher evidence fix is already registered for any products in the GPA product list
                    if (m_products.Any(product => product.DisableGeneratePublisherEvidence != 0))
                    {
                        PublisherEvidenceCheckBox.Checked = true;

                        // Determine whether any GPA services have been installed or uninstalled since the last time the fix was applied
                        if (m_products.Any(product => product.DisableGeneratePublisherEvidence == 0))
                        {
                            AppendStatusMessage("NoInetFixUtil has detected that one or more GPA" +
                                " products have been installed since the publisher evidence fix was last applied." +
                                " To correct this, double-click the \"Disable publisher evidence generation\" checkbox.");
                        }
                    }
                }
                else
                {
                    // No GPA products are installed
                    m_products = new List<Product>();
                }
            }

            AppendStatusMessage("Done.");

            // Determine if the client OIDs are already registered on this system
            if (ClientOIDs.All(IsRegistered))
                ClientOIDCheckBox.Checked = true;

            // Determine if the root certificate list auto update feature is already disabled on this system
            using (RegistryKey authRootKey = Registry.LocalMachine.OpenSubKey(@"Software\Policies\Microsoft\SystemCertificates\AuthRoot"))
            {
                if ((object)authRootKey != null)
                {
                    disableRootAutoUpdate = authRootKey.GetValue("DisableRootAutoUpdate");

                    if ((object)disableRootAutoUpdate != null && !Equals(disableRootAutoUpdate, 0))
                        RootCertificateListCheckBox.Checked = true;
                }
            }

            // Enabled checkbox checked events
            m_checkedEventsEnabled = true;

            if (applyAll)
            {
                if (!ServiceOIDCheckBox.Checked)
                    ServiceOIDCheckBox.Checked = true;
                if (!PublisherEvidenceCheckBox.Checked)
                    PublisherEvidenceCheckBox.Checked = true;
                if (!ClientOIDCheckBox.Checked)
                    ClientOIDCheckBox.Checked = true;
                if (!RootCertificateListCheckBox.Checked)
                    RootCertificateListCheckBox.Checked = true;



            }
        }

        // Registers or unregisters the OIDs used by GPA services based on user selection.
        private void ServiceOIDCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (!m_checkedEventsEnabled)
                return;

            if (ServiceOIDCheckBox.Checked)
            {
                foreach (Product product in m_products)
                    RegisterServiceOID(product);
            }
            else
            {
                foreach (Product product in m_products)
                    UnregisterServiceOID(product);
            }
        }

        // Registers or unregisters the OIDs used by GPA client applications based on user selection.
        private void ClientOIDCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (!m_checkedEventsEnabled)
                return;

            if (ClientOIDCheckBox.Checked)
            {<commit_msg>Added Try Catch to fail silently<commit_after>﻿//******************************************************************************************************
//  Main.cs - Gbtc
//
//  Copyright © 2013, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
//  not use this file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://www.opensource.org/licenses/MIT
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  12/20/2013 - <NAME>
//       Generated original version of source code.
//
//******************************************************************************************************

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.Win32;

namespace NoInetFixUtil
{
    public partial class Main : Form
    {
        #region [ Members ]

        // Nested Types
        private class Product
        {
            public string Name;
            public string InstallPath;
            public string ServiceOID;
            public int DisableGeneratePublisherEvidence;
        }

        // Constants
        private static readonly string[] ClientOIDs = { "172.16.58.3", "1.3.6.1.5.5.7.1.1" };

        // Fields
        private List<Product> m_products;
        private bool m_checkedEventsEnabled;
        private bool applyAll;
        #endregion

        #region [ Constructors ]

        public Main(bool applyAll)
        {
            InitializeComponent();
            this.applyAll = applyAll;
        }

        #endregion

        #region [ Methods ]

        // Sets the initial state of the checkboxes at startup.
        private void Main_Load(object sender, EventArgs e)
        {
            object disableRootAutoUpdate;

            StatusTextBox.AppendText("Detecting GPA products that are installed on the system... ");

            using (RegistryKey gpaKey = Registry.LocalMachine.OpenSubKey(@"Software\Grid Protection Alliance"))
            {
                if ((object)gpaKey != null)
                {
                    // Populate the list of GPA products installed on the system
                    m_products = gpaKey.GetSubKeyNames()
                        .Select(productName => ConvertToProductAndDispose(gpaKey.OpenSubKey(productName)))
                        .ToList();

                    // Display the list of products that will be affected by the fixes that this tool provides
                    foreach (Product product in m_products)
                    {
                        GPAProductsTextBox.AppendText(Environment.NewLine);
                        GPAProductsTextBox.AppendText(product.Name);
                    }

                    // Determine whether the service OIDs are already registered for any products in the GPA product list
                    if (m_products.Any(product => (object)product.ServiceOID != null))
                    {
                        ServiceOIDCheckBox.Checked = true;

                        // Determine whether any GPA services have been installed or uninstalled since the last time the fix was applied
                        if (m_products.Any(product => (object)product.ServiceOID == null))
                        {
                            AppendStatusMessage("NoInetFixUtil has detected that one or more GPA" +
                                " products have been installed since the service OID fix was last applied." +
                                " To correct this, double-click the \"Register OIDs used by GSF services\" checkbox.");
                        }
                        else if (m_products.Any(product => (object)product.ServiceOID != null && (object)product.InstallPath == null))
                        {
                            AppendStatusMessage("NoInetFixUtil has detected that the service OID fix" +
                                " has been applied to one or more GPA products that have since been uninstalled." +
                                " To correct this, double-click the \"Register OIDs used by GSF services\" checkbox.");
                        }
                    }

                    // Determine whether the publisher evidence fix is already registered for any products in the GPA product list
                    if (m_products.Any(product => product.DisableGeneratePublisherEvidence != 0))
                    {
                        PublisherEvidenceCheckBox.Checked = true;

                        // Determine whether any GPA services have been installed or uninstalled since the last time the fix was applied
                        if (m_products.Any(product => product.DisableGeneratePublisherEvidence == 0))
                        {
                            AppendStatusMessage("NoInetFixUtil has detected that one or more GPA" +
                                " products have been installed since the publisher evidence fix was last applied." +
                                " To correct this, double-click the \"Disable publisher evidence generation\" checkbox.");
                        }
                    }
                }
                else
                {
                    // No GPA products are installed
                    m_products = new List<Product>();
                }
            }

            AppendStatusMessage("Done.");

            // Determine if the client OIDs are already registered on this system
            if (ClientOIDs.All(IsRegistered))
                ClientOIDCheckBox.Checked = true;

            // Determine if the root certificate list auto update feature is already disabled on this system
            using (RegistryKey authRootKey = Registry.LocalMachine.OpenSubKey(@"Software\Policies\Microsoft\SystemCertificates\AuthRoot"))
            {
                if ((object)authRootKey != null)
                {
                    disableRootAutoUpdate = authRootKey.GetValue("DisableRootAutoUpdate");

                    if ((object)disableRootAutoUpdate != null && !Equals(disableRootAutoUpdate, 0))
                        RootCertificateListCheckBox.Checked = true;
                }
            }

            // Enabled checkbox checked events
            m_checkedEventsEnabled = true;

            if (applyAll)
            {
                try
                {
                    if (!ServiceOIDCheckBox.Checked)
                        ServiceOIDCheckBox.Checked = true;
                    if (!PublisherEvidenceCheckBox.Checked)
                        PublisherEvidenceCheckBox.Checked = true;
                    if (!ClientOIDCheckBox.Checked)
                        ClientOIDCheckBox.Checked = true;
                    if (!RootCertificateListCheckBox.Checked)
                        RootCertificateListCheckBox.Checked = true;

                }
                catch { }

                this.Close();

            }
        }

        // Registers or unregisters the OIDs used by GPA services based on user selection.
        private void ServiceOIDCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (!m_checkedEventsEnabled)
                return;

            if (ServiceOIDCheckBox.Checked)
            {
                foreach (Product product in m_products)
                    RegisterServiceOID(product);
            }
            else
            {
                foreach (Product product in m_products)
                    UnregisterServiceOID(product);
            }
        }

        // Registers or unregisters the OIDs used by GPA client applications based on user selection.
        private void ClientOIDCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (!m_checkedEventsEnabled)
                return;

            if (ClientOIDCheckBox.Checked)
            {
{'fraction_non_alphanumeric': 0.10023894166326709, 'fraction_numerical': 0.0043126056296987, 'mean_word_length': 1.6254589963280295, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2743889', 'n_tokens_mistral': 3916, 'n_tokens_neox': 3566, 'n_words': 1234}