/// <summary>
 /// ApplyCertificatesACL method implementation
 /// </summary>
 public bool UpdateCertificatesACL(KeyMgtOptions options)
 {
     try
     {
         return(SIDs.internalUpdateCertificatesACLs(options));
     }
     catch (Exception e)
     {
         _log.WriteEntry(string.Format("Error on WebAdminService Service ApplyCertificatesACL ACL method : {0}.", e.Message), EventLogEntryType.Error, 2010);
         throw e;
     }
 }
 /// <summary>
 /// ApplyDirectoriesACL method implmentation
 /// </summary>
 public void UpdateDirectoriesACL(string path)
 {
     try
     {
         SIDs.InternalUpdateDirectoryACLs(path);
     }
     catch (Exception e)
     {
         _log.WriteEntry(string.Format("Error on WebAdminService Service ApplyDirectoriesACL ACL method : {0}.", e.Message), EventLogEntryType.Error, 2010);
         throw e;
     }
 }
        /// <summary>
        /// GetRemoteSIDsInformations method implmentation
        /// </summary>
        private SIDsParametersRecord GetRemoteSIDsInformations(Dictionary <string, bool> servers)
        {
            SIDsParametersRecord retvalue = null;

            try
            {
                retvalue = SIDs.GetSIDs();
                if (retvalue == null)
                {
                    string fqdn = Dns.GetHostEntry("localhost").HostName;
                    foreach (var srv in servers)
                    {
                        if (srv.Key.ToLower().Equals(fqdn.ToLower()) || (!srv.Value))
                        {
                            continue;
                        }
                        WebAdminClient manager = new WebAdminClient();
                        manager.Initialize(srv.Key);
                        try
                        {
                            IWebAdminServices client = manager.Open();
                            try
                            {
                                retvalue = client.RequestSIDsInformations();
                                SIDs.Assign(retvalue);
                                break; // Break on first primary server;
                            }
                            finally
                            {
                                manager.Close(client);
                            }
                        }
                        catch (Exception e)
                        {
                            _log.WriteEntry(string.Format("Error on WebAdminService Service GetRemoteSIDsInformations method : {0} / {1}.", srv, e.Message), EventLogEntryType.Error, 2010);
                        }
                        finally
                        {
                            manager.UnInitialize();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _log.WriteEntry(string.Format("Error on WebAdminService Service GetLocalSIDsInformations method : {0}.", e.Message), EventLogEntryType.Error, 2010);
                throw e;
            }
            return(retvalue);
        }
        /// <summary>
        /// RequestSIDsInformations method implementation
        /// </summary>
        public SIDsParametersRecord RequestSIDsInformations()
        {
            SIDsParametersRecord retvalue = null;

            try
            {
                retvalue = SIDs.Initialize();
            }
            catch (Exception e)
            {
                _log.WriteEntry(string.Format("Error on WebAdminService Service RequestSIDsInformations method : {0}.", e.Message), EventLogEntryType.Error, 2010);
                throw e;
            }
            return(retvalue);
        }
        /// <summary>
        /// PushCertificate method implmentation
        /// </summary>
        internal void PushCertificate(string cert)
        {
            X509Certificate2 x509 = new X509Certificate2(Convert.FromBase64String(cert), "", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);

            try
            {
                if (x509 == null)
                {
                    return;
                }
                X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                store.Open(OpenFlags.MaxAllowed);
                store.Add(x509);
                store.Close();
            }
            finally
            {
                Certs.CleanSelfSignedCertificate(x509, StoreLocation.LocalMachine);
                x509.Reset();
                SIDs.internalUpdateCertificatesACLs(KeyMgtOptions.AllCerts);
            }
        }
 /// <summary>
 /// WriteConfigurationToCache method implementation
 /// </summary>
 internal void WriteConfigurationToCache(byte[] config)
 {
     try
     {
         using (FileStream fs = new FileStream(CFGUtilities.ConfigCacheFile, FileMode.Create, FileAccess.ReadWrite))
         {
             fs.Write(config, 0, config.Length);
             fs.Close();
         }
         if (SIDs.Loaded)
         {
             XmlConfigSerializer xmlserializer = new XmlConfigSerializer(typeof(SIDsParametersRecord));
             MemoryStream        stm           = new MemoryStream();
             byte[] bytes = null;
             using (StreamReader reader = new StreamReader(stm))
             {
                 xmlserializer.Serialize(stm, SIDs.GetSIDs());
                 stm.Position = 0;
                 using (AESSystemEncryption aes = new AESSystemEncryption())
                 {
                     bytes = aes.Encrypt(stm.ToArray());
                 }
             }
             using (FileStream fs = new FileStream(SystemUtilities.SystemCacheFile, FileMode.Create, FileAccess.ReadWrite))
             {
                 fs.Write(bytes, 0, bytes.Length);
                 fs.Close();
             }
         }
     }
     catch (Exception e)
     {
         _log.WriteEntry(string.Format("Error on WebAdminService Service WriteConfigurationToCache method : {0}.", e.Message), EventLogEntryType.Error, 2010);
         throw e;
     }
 }
        /// <summary>
        /// CreateRSACertificateForSQLEncryption method implementation
        /// </summary>
        internal string CreateRSACertificateForSQLEncryption(Dictionary <string, bool> servers, string subject, int years)
        {
            SIDs.Initialize();

            string thumbprint = null;

            try
            {
                string           strcert = string.Empty;
                X509Certificate2 cert    = null;
                try
                {
                    cert = Certs.CreateRSACertificateForSQLEncryption(subject, years, out strcert);
                    if (cert == null)
                    {
                        return(null);
                    }
                    else
                    {
                        thumbprint = cert.Thumbprint;
                    }
                }
                finally
                {
                    cert.Reset();
                }

                SIDs.internalUpdateCertificatesACLs(KeyMgtOptions.MFACerts);

                string        fqdn        = Dns.GetHostEntry("localhost").HostName;
                List <string> servernames = (from server in servers
                                             where (server.Key.ToLower() != fqdn.ToLower())
                                             select server.Key.ToLower()).ToList <string>();
                foreach (string srv in servernames)
                {
                    WebAdminClient manager = new WebAdminClient();
                    manager.Initialize(srv);
                    try
                    {
                        IWebAdminServices client = manager.Open();
                        try
                        {
                            client.PushCertificate(strcert);
                        }
                        finally
                        {
                            manager.Close(client);
                        }
                    }
                    catch (Exception e)
                    {
                        _log.WriteEntry(string.Format("Error on WebAdminService Service CreateRSACertificateForSQLEncryption method : {0} / {1}.", srv, e.Message), EventLogEntryType.Error, 2010);
                    }
                    finally
                    {
                        manager.UnInitialize();
                    }
                }
            }
            catch (Exception e)
            {
                _log.WriteEntry(string.Format("Error on WebAdminService Service CreateRSACertificateForSQLEncryption method : {0}.", e.Message), EventLogEntryType.Error, 2010);
                throw e;
            }
            return(thumbprint);
        }
 /// <summary>
 /// PushSIDsInformations method implementation
 /// </summary>
 public void PushSIDsInformations(SIDsParametersRecord rec)
 {
     SIDs.Assign(rec);
 }